题目描述:http://poj.org/problem?id=2192
解题思路;
1.book[i][j]表示str2的前i个元素和str1的前j个元素是否能组成Str_Link的前(i+j)个元素所组成的数组;
如果是,则book[i][j]=1,否则,book[i][j]=0;
2.book[0][j]表示str1的前j个元素是否能组成Str_Link的前j个元素所组成的数组;
book[i][0]表示str2的前i个元素是否能组成Str_Link的前i个元素所组成的数组;
3.状态转移方程:
if(!book[i-1][j]&&!book[i][j-1]) continue;
if(book[i-1][j]&&str2[i]==Str_Link[i+j]) book[i][j]=1;
if(book[i][j-1]&&str1[j]==Str_Link[i+j]) book[i][j]=1;
例子分析:
str1:47935 str2:25645439 Str_Link:2564745934359
book的初始状态:
松弛后的book:
AC代码:
1 #include<stdio.h> 2 #include<string.h> 3 4 char str1[202],str2[202]; 5 char Str_Link[404]; 6 7 int book[202][202]; 8 int stL1,stL2; 9 10 void get_book()//初始化book 11 { 12 13 int i; 14 15 stL1=strlen(str1+1); 16 stL2=strlen(str2+1); 17 18 for(i=1;i<=stL1;i++) 19 { 20 21 if(book[0][i-1]&&str1[i]==Str_Link[i]) book[0][i]=1; 22 else break; 23 24 } 25 26 for(i=1;i<=stL2;i++) 27 { 28 29 if(book[i-1][0]&&str2[i]==Str_Link[i]) book[i][0]=1; 30 else break; 31 32 } 33 34 35 } 36 37 int main() 38 { 39 40 int T,count=0; 41 int i,j; 42 43 scanf("%d",&T); 44 45 while(T--){ 46 47 count++; 48 memset(book,0,sizeof(book)); 49 50 book[0][0]=1; 51 52 scanf("%s%s%s",str1+1,str2+1,Str_Link+1); 53 54 get_book(); 55 56 for(i=1;i<=stL2;i++) 57 { 58 59 for(j=1;j<=stL1;j++) 60 { 61 62 if(!book[i-1][j]&&!book[i][j-1]) continue; 63 64 if(book[i-1][j]&&str2[i]==Str_Link[i+j]) book[i][j]=1; 65 66 if(book[i][j-1]&&str1[j]==Str_Link[i+j]) book[i][j]=1; 67 68 } 69 70 } 71 72 if(book[stL2][stL1]) printf("Data set %d: yes\n",count); 73 else printf("Data set %d: no\n",count); 74 75 } 76 77 return 0; 78 79 } 80 81 /* 82 83 1 84 85 47935 86 87 25645439 88 89 2564745934359 90 91 92 */
测试数据:
时间: 2024-11-06 03:13:46