hdu1501 dp:两个字符串能否组成新串,状态表示能,不能。
dp思路最重要!!
1 #include<stdio.h> 2 #include<string.h> 3 char s1[1005],s2[1005],s[1005]; 4 int dp[1005][1005]; 5 int main() 6 { 7 int T,t,i,j,len1,len2; 8 scanf("%d",&T); 9 for (t=1;t<=T;t++) 10 { 11 scanf("%s%s%s",s1,s2,s); 12 len1=strlen(s1); len2=strlen(s2); 13 for (i=len1+len2;i>=1;i--) 14 { 15 s1[i]=s1[i-1]; 16 s2[i]=s2[i-1]; 17 s[i]=s[i-1]; 18 } 19 for (i=0;i<=len1;i++) 20 for (j=0;j<=len2;j++) 21 if (i==0&&j==0) dp[i][j]=1; 22 else if (i==0) dp[i][j]=dp[i][j-1]&&s2[j]==s[j]; 23 else if (j==0) dp[i][j]=dp[i-1][j]&&s1[i]==s[i]; 24 else dp[i][j]=(dp[i-1][j]&&s1[i]==s[i+j])||(dp[i][j-1]&&s2[j]==s[i+j]); 25 if (dp[len1][len2]) printf("Data set %d: yes\n",t); 26 else printf("Data set %d: no\n",t); 27 } 28 }