# hdu4323Magic Number(dp）

http://acm.hdu.edu.cn/showproblem.php?pid=4323

 1 #include <iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 #include<stdlib.h>
6 using namespace std;
7 int dp[12][12],kk[1510],num[1010];
8 char s[1510][12],ss[12];
9 int main()
10 {
11     int i,j,k,n,m,t,a,b,o=0,g;
12     scanf("%d",&t);
13     while(t--)
14     {
15         o++;
16         scanf("%d%d",&n,&m);
17         for(i = 1; i <= n ;i++)
18         {
19             scanf("%s",s[i]);
20             kk[i] = strlen(s[i]);
21         }
22         for(i = 1 ; i <= m ; i++)
23         {
24             scanf("%s %d",ss,&g);
25             num[i]=0;
26             k = strlen(ss);
27             for(j = 1; j <=  n ; j++)
28             {
29                 if(abs(k-kk[j])>g)
30                 continue;
31                 int f=1;
32                 memset(dp,0,sizeof(dp));
33                 for(a = 1 ; a <= k ; a++)
34                 dp[a][0] = a;
35                 for(b = 1 ; b <= kk[j] ; b++)
36                 dp[0][b] = b;
37                 for(a = 1 ; a <= k ; a++)
38                     for(b = 1 ; b <= kk[j] ; b++)
39                     {
40                         if(ss[a-1]==s[j][b-1])
41                         dp[a][b] = dp[a-1][b-1];
42                         else
43                         dp[a][b] = dp[a-1][b-1]+1;
44                         dp[a][b] = min(dp[a][b],min(dp[a][b-1]+1,dp[a-1][b]+1));
45                     }
46                 if(dp[k][kk[j]]<=g)
47                 num[i]++;
48             }
49         }
50         printf("Case #%d:\n",o);
51         for(i = 1; i <= m ; i++)
52         printf("%d\n",num[i]);
53     }
54     return 0;
55 }
View Code

posted @ 2013-08-12 19:39  _雨  阅读(199)  评论(0编辑  收藏  举报