题意:
从a串和b串中顺序地取出是否能组成c串;
思路:
dp[i][j] 代表从a串中取了i个,b串中取了j个的情况;
#include <bits/stdc++.h> using namespace std; typedef long long LL; char a[220]; char b[220]; char c[420]; int dp[220][220]; int main() { int T,cas=1; scanf("%d",&T); while(T--) { scanf("%s%s%s",a+1,b+1,c+1); int len_a,len_b,len_c; len_a=strlen(a+1); len_b=strlen(b+1); memset(dp,0,sizeof(dp)); for(int i=0;i<=len_a;i++) { if(a[i]==c[i]) dp[i][0]=1; } for(int i=0;i<=len_b;i++) { if(b[i]==c[i]) dp[0][i]=1; } for(int i=1;i<=len_a;++i) { for(int j=1;j<=len_b;++j) { if(dp[i][j-1]&&b[j]==c[i+j]) dp[i][j]=1; if(dp[i-1][j]&&a[i]==c[i+j]) dp[i][j]=1; } } printf("Data set %d: ",cas++); if(dp[len_a][len_b]) puts("yes"); else puts("no"); } return 0; }