HDU 4323 Magic Number(DP)
先是经典的DP,敲的不熟啊,很久没打了吗?初始化都忘了,调试了会,然后过了样例各种WA,不过也应该,那次多校完,就听说是编辑距离,就按照等于算的,看了下题解是小于等于,越来越不愿意看题了。
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <math.h>
5 #define N 100000000
6 int min(int x,int y,int z)
7 {
8 int q;
9 q = x;
10 if(q > y)
11 q = y;
12 if(q > z)
13 q = z;
14 return q;
15 }
16 char p[1501][11],str[11];
17 int num[1501],o[11][11];
18 int main()
19 {
20 int t,n,m,i,j,nu = 0,x,key,len;
21 int a,b;
22 scanf("%d",&t);
23 while(t--)
24 {
25 nu ++;
26 memset(p,0,sizeof(p));
27 memset(str,0,sizeof(str));
28 memset(num,0,sizeof(num));
29 scanf("%d%d%*c",&n,&m);
30 for(i = 1; i <= n; i ++)
31 {
32 gets(p[i]);
33 num[i] = strlen(p[i]);
34 }
35 printf("Case #%d:\n",nu);
36 for(i = 1; i <= m; i ++)
37 {
38 scanf("%s%d",str,&x);
39 key = 0;
40 len = strlen(str);
41 for(j = 1; j <= n; j ++)
42 {
43 memset(o,0,sizeof(o));
44 if(abs(len-num[j]) > x)
45 continue;
46 for(a = 1; a <= len; a ++)
47 o[a][0] = a;
48 for(a = 1;a <= num[j];a ++)
49 o[0][a] = a;
50 for(a = 1; a <= len; a ++)
51 {
52 for(b = 1; b <= num[j]; b ++)
53 {
54 if(str[a-1] == p[j][b-1])
55 o[a][b] = o[a-1][b-1];
56 else
57 o[a][b] = min(o[a-1][b-1]+1,o[a-1][b]+1,o[a][b-1]+1);
58 }
59 }
60 if(o[len][num[j]] <= x)
61 key ++;
62 }
63 printf("%d\n",key);
64 }
65 }
66 return 0;
67 }
2 #include <string.h>
3 #include <stdlib.h>
4 #include <math.h>
5 #define N 100000000
6 int min(int x,int y,int z)
7 {
8 int q;
9 q = x;
10 if(q > y)
11 q = y;
12 if(q > z)
13 q = z;
14 return q;
15 }
16 char p[1501][11],str[11];
17 int num[1501],o[11][11];
18 int main()
19 {
20 int t,n,m,i,j,nu = 0,x,key,len;
21 int a,b;
22 scanf("%d",&t);
23 while(t--)
24 {
25 nu ++;
26 memset(p,0,sizeof(p));
27 memset(str,0,sizeof(str));
28 memset(num,0,sizeof(num));
29 scanf("%d%d%*c",&n,&m);
30 for(i = 1; i <= n; i ++)
31 {
32 gets(p[i]);
33 num[i] = strlen(p[i]);
34 }
35 printf("Case #%d:\n",nu);
36 for(i = 1; i <= m; i ++)
37 {
38 scanf("%s%d",str,&x);
39 key = 0;
40 len = strlen(str);
41 for(j = 1; j <= n; j ++)
42 {
43 memset(o,0,sizeof(o));
44 if(abs(len-num[j]) > x)
45 continue;
46 for(a = 1; a <= len; a ++)
47 o[a][0] = a;
48 for(a = 1;a <= num[j];a ++)
49 o[0][a] = a;
50 for(a = 1; a <= len; a ++)
51 {
52 for(b = 1; b <= num[j]; b ++)
53 {
54 if(str[a-1] == p[j][b-1])
55 o[a][b] = o[a-1][b-1];
56 else
57 o[a][b] = min(o[a-1][b-1]+1,o[a-1][b]+1,o[a][b-1]+1);
58 }
59 }
60 if(o[len][num[j]] <= x)
61 key ++;
62 }
63 printf("%d\n",key);
64 }
65 }
66 return 0;
67 }