Magic Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1113 Accepted Submission(s): 480
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).
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
-----------------
直接dp用C++提交可以水过...
----------------
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=1111; char words[2222][22]; int f[22][22]; char s[22]; int main() { int T; int n,m; int thr; int len1,len2; int ans; scanf("%d",&T); for (int lp=1;lp<=T;lp++) { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) { scanf("%s",words[i]+1); } printf("Case #%d:\n",lp); while (m--) { ans=0; scanf("%s%d",s+1,&thr); len1=strlen(s+1); for (int p=1;p<=n;p++) { len2=strlen(words[p]+1); for (int i=0;i<=len1;i++) f[i][0]=i; for (int i=0;i<=len2;i++) f[0][i]=i; for (int i=1;i<=len1;i++) { for (int j=1;j<=len2;j++) { f[i][j]=min(f[i-1][j]+1,f[i][j-1]+1); if (s[i]==words[p][j]) f[i][j]=min(f[i][j],f[i-1][j-1]); else f[i][j]=min(f[i][j],f[i-1][j-1]+1); } } if (f[len1][len2]<=thr) ans++; } printf("%d\n",ans); } } return 0; }