首页 > ACM题库 > HDU-杭电 > HDU 4323-Magic Number-动态规划-[解题报告]HOJ
2015
05-23

HDU 4323-Magic Number-动态规划-[解题报告]HOJ

Magic Number

问题描述 :

There are many magic numbers whose lengths are less than 10. Given some queries, each contains a single number, if the Levenshtein distance (see below) between the number in the query and a magic number is no more than a threshold, we call the magic number is the lucky number for that query. Could you find out how many luck numbers are there for each query?

Levenshtein distance (from Wikipedia http://en.wikipedia.org/wiki/Levenshtein_distance):
In information theory and computer science, the Levenshtein distance is a string metric for measuring the amount of difference between two sequences. The term edit distance is often used to refer specifically to Levenshtein distance.
The Levenshtein distance between two strings is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character. It is named after Vladimir Levenshtein, who considered this distance in 1965.
For example, the Levenshtein distance between "kitten" and "sitting" is 3, since the following three edits change one into the other, and there is no way to do it with fewer than three edits:
1.kitten → sitten (substitution of ‘s’ for ‘k’)
2.sitten → sittin (substitution of ‘i’ for ‘e’)
3.sittin → sitting (insertion of ‘g’ at the end).

输入:

There are several test cases. The first line contains a single number T shows that there are T cases. For each test case, there are 2 numbers in the first line: n (n <= 1500) m (m <= 1000) where n is the number of magic numbers and m is the number of queries.
In the next n lines, each line has a magic number. You can assume that each magic number is distinctive.
In the next m lines, each line has a query and a threshold. The length of each query is no more than 10 and the threshold is no more than 3.

输出:

There are several test cases. The first line contains a single number T shows that there are T cases. For each test case, there are 2 numbers in the first line: n (n <= 1500) m (m <= 1000) where n is the number of magic numbers and m is the number of queries.
In the next n lines, each line has a magic number. You can assume that each magic number is distinctive.
In the next m lines, each line has a query and a threshold. The length of each query is no more than 10 and the threshold is no more than 3.

样例输入:

1
5 2
656
67
9313
1178
38
87 1
9509 1

样例输出:

Case #1:
1
0

#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<cstdio>
#include<string>
using namespace std;
char str[1509][1009],s[1009];
int dp[1509][1509];
int n,m;
int main()
{
    int ca,len,len2,p,op,ans;
    scanf("%d",&ca);
    for(int ii=1;ii<=ca;ii++)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%s",str[i]+1);
        }
        printf("Case #%d:\n",ii);
        while(m--)
        {
            scanf("%s%d",s+1,&op);
            ans=0;
            len=strlen(s+1);
            for(int p=0;p<n;p++)
            {
                len2=strlen(str[p]+1);
                for(int j=0;j<=len;j++)dp[0][j]=j;
                for(int j=0;j<=len2;j++)dp[j][0]=j;
                for(int i=1;i<=len;i++)
                {
                    for(int j=1;j<=len2;j++)
                    {
                        dp[j][i]=min(dp[j-1][i]+1,dp[j][i-1]+1);
                        dp[j][i]=min(dp[j][i],dp[j-1][i-1]+(str[p][j]==s[i]?0:1));
                    }
                }
               // cout<<len2<<" "<<len<<" "<<dp[len2][len]<<endl;
                if(dp[len2][len]<=op)ans++;
            }
            printf("%d\n",ans);
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/wsniyufang/article/details/7830978