poj 2192 (DP)
这个题题目意思是给你三个字符串str1,str2,str3.将str3从左自右扫描,去匹配str1和str2中的元素,不可重复,若存在一种匹配方法使得str1和str2都被匹配完全了,则输出yes,否则no
用布尔变量dp[i][j]表示组成一个字符串用了第一个字符串的i个字符,用了第二个字符串的j个字符,那么题目所要求的就是dp[len1][len2].
1 // File Name: 2192.cpp 2 // Author: Missa_Chen 3 // Created Time: 2013年07月08日 星期一 09时09分47秒 4 5 #include <iostream> 6 #include <string> 7 #include <algorithm> 8 #include <cstdio> 9 #include <cstring> 10 #include <cmath> 11 #include <queue> 12 #include <map> 13 #include <stack> 14 #include <set> 15 #include <cstdlib> 16 #include <vector> 17 #include <time.h> 18 19 using namespace std; 20 21 #define LL long long 22 const int maxn = 2e2 + 5; 23 bool solve(string s1, string s2, string s) 24 { 25 bool dp[maxn][maxn] = {0}; 26 for (int i = 1; i <= s1.size(); ++i) 27 if (s1[i - 1] == s[i - 1]) 28 dp[i][0] = 1; 29 for (int i = 1; i <= s2.size(); ++i) 30 if (s2[i - 1] == s[i - 1]) 31 dp[0][i] = 1; 32 for (int i = 1; i <= s1.size(); ++i) 33 for (int j = 1; j <= s2.size(); ++j) 34 { 35 if ((s1[i - 1] == s[i + j - 1] && dp[i - 1][j]) || 36 (s2[j - 1] == s[i + j - 1] && dp[i][j - 1])) 37 dp[i][j] = 1; 38 } 39 return dp[s1.size()][s2.size()]; 40 } 41 int main() 42 { 43 int T; 44 cin >> T; 45 for (int i = 1; i <= T; ++i) 46 { 47 string s1, s2, s; 48 cin >> s1 >> s2 >> s; 49 cout << "Data set "<< i <<": "; 50 if (solve(s1, s2, s)) cout <<"yes" << endl; 51 else cout << "no" << endl; 52 } 53 return 0; 54 }