标签:
还想用hash记录……果然是天真。lca转移比较简单,每次增加1。每次找是当前-1的就行了。
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 20 using namespace std; 21 22 const int mod = 100007; 23 const int maxn = 1010; 24 char a[maxn]; 25 char b[maxn]; 26 int dp[maxn][maxn]; 27 int fa[mod]; 28 int na, nb; 29 30 int main() { 31 // freopen("in", "r", stdin); 32 while(~scanf("%s %s", a, b)) { 33 memset(dp, 0, sizeof(dp)); 34 memset(fa, -1, sizeof(fa)); 35 na = strlen(a); 36 nb = strlen(b); 37 for(int i = 0; i < na; i++) { 38 for(int j = 0; j < nb; j++) { 39 if(a[i] == b[j]) { 40 if(dp[i][j] + 1 > max(dp[i][j+1], dp[i+1][j])) { 41 dp[i+1][j+1] = dp[i][j] + 1; 42 // int cur = (((i + 1) * 59) % mod + ((j + 1) * 61) % mod) % mod; 43 // int pre = ((i * 59) % mod + (j * 61) % mod) % mod; 44 // fa[cur][0] = pre; 45 // fa[cur][1] = i; 46 // ii = cur; 47 } 48 else dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]); 49 } 50 else { 51 dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]); 52 } 53 } 54 } 55 int ii = na; 56 int jj = nb; 57 char st[maxn]; 58 int top = 0; 59 while(dp[ii][jj]) { 60 if(dp[ii][jj] == dp[ii-1][jj]) ii--; 61 else if(dp[ii][jj] == dp[ii][jj-1]) jj--; 62 else { 63 ii--, jj--; 64 st[top++] = a[ii]; 65 } 66 67 } 68 while(top) printf("%c", st[--top]); 69 printf("\n"); 70 } 71 return 0; 72 }
标签:
原文地址:http://www.cnblogs.com/vincentX/p/5406496.html