poj 1934 Trip (LCS)
来源:互联网 发布:id for mac中文破解版 编辑:程序博客网 时间:2024/04/30 02:19
/*按升序输出两个串所有的最长公共子序列首先求出最长公共子序列,然后处理处两个数组,f1['a'~'z'][j]表示该字母在第一个串的前j个字母中出现的最大下标,f2['a'~'z'][j]表示该字母在第二个串的前j个字母中出现的最大下标。现在我们已经知道了最长公共子序列的长度,我们从最后一位开始枚举每一位放什么字母,用dfs来实现,并用前面处理出来的数组进行可行性剪枝。由于最后答案的串不超过1000个,所以暴搜的复杂度可以接受。*/# include <stdio.h># include <algorithm># include <iostream># include <string.h># include <vector># include <string>using namespace std;int dp[110][110];int f1[30][110];int f2[30][110];vector<string>ans;void dfs(int x,int y,string s,int L){ if(x<0||y<0) return ; if(L<=0)//搜到了最底层,得到一个解 { ans.push_back(s); return ; } char ch; for(int i=0; i<26; i++) { int x1=f1[i][x]; int x2=f2[i][y]; if(dp[x1][x2]==L)//第L位放(i+'a')字母可行 { ch='a'+i; dfs(x1-1,x2-1,ch+s,L-1); } }}int main(){ string a,b; int i,j,lena,lenb; while(cin>>a>>b) { ans.clear(); lena=a.size(); lenb=b.size(); memset(dp,0,sizeof(dp)); for(i=1; i<=lena; i++) { for(j=1; j<=lenb; j++) { if(a[i-1]==b[j-1]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); } } for(i=0; i<26; i++) { f1[i][0]=0; for(j=1; j<=lena; j++) { if(a[j-1]==i+'a') f1[i][j]=j; else f1[i][j]=f1[i][j-1]; } } for(i=0; i<26; i++) { f2[i][0]=0; for(j=1; j<=lenb; j++) { if(b[j-1]==i+'a') f2[i][j]=j; else f2[i][j]=f2[i][j-1]; } } // printf("%d\n",dp[lena][lenb]); dfs(lena,lenb,"",dp[lena][lenb]); sort(ans.begin(),ans.end()); for(i=0; i<(int)ans.size(); i++) cout<<ans[i]<<endl; } return 0;}
0 0
- poj 1934 Trip (LCS)
- POJ 1934 Trip(LCS+枚举+输出所有路径)
- POJ 1934 Trip DP
- poj 1934 Trip
- POJ 1934 Trip 笔记
- POJ1934:Trip(LCS)
- POJ 1934 【LCS加强版。。】
- POJ1934:Trip(LCS + 所有路径打印)
- POJ 1734 Sightseeing trip
- POJ 1734-Sightseeing trip
- poj 3301 Texas Trip
- POJ 3301 Texas Trip
- POJ 2646 The Trip
- POJ 3301 Texas Trip
- POJ 3301Texas Trip
- Texas Trip+POJ+三分
- poj 3301 Texas Trip
- POJ 3301 Texas Trip
- 两种快速排序算法比较
- poj1845 Sumdiv
- Python:第一天
- 千万别用MongoDB?真的吗?!
- 1502021733-hdu-How Many Tables
- poj 1934 Trip (LCS)
- 内核相关网站——TMP
- Android中实现文件下载
- C/C++指针
- iOS 学习资料整理
- Unity3D 相机控制
- 【Android Tips】Timestamp在Android上的转换
- 堆排序(c实现)
- 滚动条