poj1934-打印LCS全部路径+搜索
来源:互联网 发布:矩阵论 清华 编辑:程序博客网 时间:2024/04/29 06:51
题目连接
题意:
打印全部LCS路径,并按照字典顺序输出。
本题比较难。
参考神人代码
稍做改进如下:
#include <stdio.h>#include <string.h>#include <math.h>#include <string>#include <set>using namespace std;const int N=100;set<string> SET;char s1[N],s2[N],temp[N];int len1,len2,longest;int last1[N][27],last2[N][27],dp[N][N];int max(int a,int b){ return a>b?a:b;}void DFS(int x,int y,int len){ if(len<=0) { SET.insert(&temp[1]); return ; } if(x>0 && y>0) for(int i=0;i<26;i++) { int t1=last1[x][i]; int t2=last2[y][i]; if(dp[t1][t2]==len) { temp[len]='a'+i; DFS(t1-1,t2-1,len-1); } }}void solve(){ int i,j; for(i=1;i<=len1;i++) for(j=1;j<=len2;j++) { if(s1[i]==s2[j]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); } longest=dp[len1][len2]; for(i=1;i<=len1;i++) for(j=0;j<26;j++) { if(s1[i]=='a'+j) last1[i][j]=i; else last1[i][j]=last1[i-1][j]; } for(i=1;i<=len2;i++) for(j=0;j<26;j++) { if(s2[i]=='a'+j) last2[i][j]=i; else last2[i][j]=last2[i-1][j]; } temp[longest+1]='\0'; DFS(len1,len2,longest); set<string>::iterator it; for(it=SET.begin();it!=SET.end();it++) { printf("%s\n",(*it).c_str()); }}int main(){ scanf("%s %s",&s1[1],&s2[1]); len1=strlen(&s1[1]); len2=strlen(&s2[1]); solve(); return 0;}
- poj1934-打印LCS全部路径+搜索
- POJ1934:Trip(LCS + 所有路径打印)
- POJ1934:Trip(LCS)
- poj2250-打印单一LCS路径。
- poj1934
- poj1934
- poj 2250 (LCS 需打印路径)
- UVA 531 - Compromise(dp + LCS打印路径)
- uva 531 - Compromise(LCS+打印路径)
- UVA 531 DP LCS 打印路径
- UVA 531 Compromise(LCS算法+打印路径)
- uva531- Compromise(lcs+打印路径)
- Light oj 1110 LCS带打印路径
- HDU1503:Advanced Fruits(LCS+打印路径)
- uva 11404 LCS打印字典序最小路径
- Pku oj 2250 Compromise(LCS+打印路径)
- 最长公共子序列 (LCS) 学习小记 Hdu 1159 + Poj 2250 (LCS路径打印)
- 广度优先搜索(BFS)+路径打印
- POJ-3026-Borg Maze
- CSS常用浮出层的写法
- 黑马程序员---工具类和设计模式
- 队列管理算法及策略总结
- nesC学习备忘录
- poj1934-打印LCS全部路径+搜索
- 六级之听力攻略1
- 流行趋势:27个引领时尚的网页设计作品【上篇】
- poj 2492 A Bug's Life
- escape加号被过滤解决方法
- springMVC详解
- 借鉴LifeAsia的table-driven 在前台POS系统 实现类似功能
- java基础 数组及数组排序方法
- css3.0 滚动的云 图形构成练习二