The input strings A, B, and C must be combined to determine whether C has the characters A and B in the original order.
Algorithm idea:
DP
Use DP [I] [J] to represent the first 0 ~ of ~ I-1 has a total of I characters and B before 0 ~ Whether the J-1 consists of a total of J characters C [I + J-1].
State transition equation:
If (I> = 1 & C [I + J-1] = A [I-1])
DP [I] [J] = DP [I] [J] | DP [I-1] [J]
If (j> = 1 & C [I + J-1] = B [J-1])
DP [I] [J] = DP [I] [J] | DP [I] [J-1]
The Code is as follows:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ char a[250]; char b[250]; char c[550]; scanf("%s%s%s",a,b,c); int la=strlen(a); int lb=strlen(b); int lc=strlen(c); int dp[250][250]; memset(dp,0,sizeof(dp)); dp[0][0]=1; if(a[0]==c[0])dp[1][0]=1; if(b[0]==c[0])dp[0][1]=1; for(int j=0;j<=la;j++){ for(int k=0;k<=lb;k++){ if(k>=1&&c[k+j-1]==b[k-1]) dp[j][k]=dp[j][k]||dp[j][k-1]; if(j>=1&&c[k+j-1]==a[j-1]) dp[j][k]=dp[j][k]||dp[j-1][k]; } } if(dp[la][lb])cout<<"Data set "<<i<<": yes"<<endl; else cout<<"Data set "<<i<<": no"<<endl; } return 0;}
ACM poj 2192 zipper