这题有两种解题思路,一个是记忆化搜索,一个是dp。
分别贴代码:
记忆化搜索:
#include#include #include #include
dp:
#include#include #include #include using namespace std;int dp[205][205];int main(){ int i,j,l1,l2,sum,t,Case=1; char a[205],b[205],str[405]; scanf("%d",&t); while(t--) { scanf("%s%s%s",&a,&b,&str); l1=strlen(a); l2=strlen(b); memset(dp,0,sizeof(dp)); dp[0][0]=1; for(i=0;i<=l1;i++) for(j=0;j<=l2;j++) { if(i>0&&a[i-1]==str[i+j-1]&&dp[i-1][j]) dp[i][j]=1; if(j>0&&b[j-1]==str[i+j-1]&&dp[i][j-1]) dp[i][j]=1; } if(dp[l1][l2]) printf("Data set %d: yes\n",Case++); else printf("Data set %d: no\n",Case++); } return 0;}