标签:poj
Description
Input
Output
Sample Input
abcabcaa acbacba
Sample Output
ababa abaca abcba acaba acaca acbaa acbca
Source
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm> using namespace std; #define ls 2*i #define rs 2*i+1 #define up(i,x,y) for(i=x;i<=y;i++) #define down(i,x,y) for(i=x;i>=y;i--) #define mem(a,x) memset(a,x,sizeof(a)) #define w(a) while(a) #define LL long long const double pi = acos(-1.0); #define N 105 #define mod 19999997 const int INF = 0x3f3f3f3f; #define exp 1e-8 char str1[N],str2[N],tmp[N]; int dp[N][N],len1,len2,len,vis1[105][30],vis2[105][30]; set<string> ans; void LCS() { int i,j; mem(dp,0); up(i,1,len1) { up(j,1,len2) { if(str1[i]==str2[j]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j] = max(dp[i][j],max(dp[i-1][j],dp[i][j-1])); } } len = dp[len1][len2]; } void solve(int l1,int l2,int len) { int i,p1,p2; if(len<=0) { ans.insert(&tmp[1]); return ; } if(l1>0 && l2>0) { up(i,0,25) { p1 = vis1[l1][i]; p2 = vis2[l2][i]; if(dp[p1][p2]==len) { tmp[len]=i+'a'; solve(p1-1,p2-1,len-1); } } } } int main() { int i,j,k; w(~scanf("%s%s",str1+1,str2+1)) { len1 = strlen(str1+1); len2 = strlen(str2+1); LCS(); mem(vis1,0); mem(vis2,0); up(i,1,len1) { up(j,0,25) { if(str1[i]==j+'a') vis1[i][j]=i; else vis1[i][j]=vis1[i-1][j]; } } up(i,1,len2) { up(j,0,25) { if(str2[i]==j+'a') vis2[i][j]=i; else vis2[i][j]=vis2[i-1][j]; } } mem(tmp,'\0'); solve(len1,len2,len); set<string>::iterator it; for(it = ans.begin(); it!=ans.end(); it++) printf("%s\n",(*it).c_str()); } return 0; }
标签:poj
原文地址:http://blog.csdn.net/libin56842/article/details/45583349