Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
也是先解LCS,再倒推回去得到合并序列,0msAC,不知道下面的为什么TLE(附代码)#include <iostream> #include <string> #include <vector> using namespace std; int main() { string s1, s2; while (cin >> s1 >> s2) { int n1 = s1.length(); int n2 = s2.length(); vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1)); vector<int> a1(n1 + 1); vector<int> a2(n2 + 1); for (int i = 1; i <= n1; ++i) for (int j = 1; j <= n2; ++j) { if (s1[i - 1] == s2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } int i = n1, j = n2; vector<char> ans; while(i > 0 && j > 0) { if(s1[i - 1] == s2[j - 1]) { ans.push_back(s1[i - 1]); i -= 1, j -= 1; } else if(dp[i][j] == dp[i - 1][j]) { ans.push_back(s1[i - 1]); i -= 1; } else if(dp[i][j] == dp[i][j - 1]) { ans.push_back(s2[j - 1]); j -= 1; } } while(i > 0) { ans.push_back(s1[i - 1]); i--; } while(j > 0) { ans.push_back(s2[j - 1]); j--; } for (int i = ans.size() - 1; i >= 0;--i) cout << ans[i]; cout << endl; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator