[POJ1934] Trip
问题描述
Alice and Bob want to go on holiday. Each of them has planned a route, which is a list of cities to be visited in a given order. A route may contain a city more than once. As they want to travel together, they have to agree on a common route. None wants to change the order of the cities on his or her route or add other cities. Therefore they have no choice but to remove some cities from the route. Of course the common route should be as long as possible. There are exactly 26 cities in the region. Therefore they are encoded on the lists as lower case letters from 'a' to 'z'.
输入格式
- The input consists of two lines; the first line is Alice's list, the second line is Bob's list.
- Each list consists of 1 to 80 lower case letters with no spaces in between.
输出格式
The output should contain all routes that meet the conditions described above, but no route should be listed more than once. Each route should be printed on a separate line. There is at least one such non-empty route, but never more than 1000 different ones. Output them in ascending order.
样例输入
abcabcaa
acbacba
样例输出
ababa
abaca
abcba
acaba
acaca
acbaa
acbca
题目大意
求两个字符串的最长公共子串,并将所有方案输出。方案之间不能重复。
题解
首先显然要用动态规划的方法把最长公共子串的长度求出来。那么怎么输出方案呢?显然动态规划的方法行不通。因此我们可以尝试构造的方法来手动构造子串。设\(f1[i][j]\)表示在第一个串中字符\(i\)在第\(j\)位前最晚出现的位置,同理也有\(f2[i][j]\)。那么利用深搜从后往前构造,dfs的参数中分别为当前在第一个串中的位置\(x\),在第二个串中的位置\(y\),构造出来的子串\(s\)和还需要构造的长度\(l\)。在每一层中从\(a\)~\(z\)枚举,找出该字符在两个串中的最近的位置(记为p1和p2)。如果该字符在两个字符串中均存在且两个字符串在p1和p2处的最长公共子序列长度不小于\(l\),那么就继续递归。最后把所有构造出的子串按字典序排序即可。
代码
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#define N 82
using namespace std;
string a,b;
int i,j,la,lb,f[N][N],f1[N][N],f2[N][N];
vector<string> v;
void dfs(int x,int y,string s,int l)
{
if(x<0||y<0) return;
if(l<=0){
v.push_back(s);
return;
}
for(int i=0;i<26;i++){
int p1=f1[i][x],p2=f2[i][y];
if(f[p1][p2]>=l){
char c='a'+i;
dfs(p1-1,p2-1,c+s,l-1);
}
}
}
int main()
{
cin>>a>>b;
la=a.length();
lb=b.length();
for(i=la;i>=1;i--) a[i]=a[i-1];
for(i=lb;i>=1;i--) b[i]=b[i-1];
for(i=1;i<=la;i++){
for(j=1;j<=lb;j++){
f[i][j]=max(f[i-1][j],f[i][j-1]);
if(a[i]==b[j]) f[i][j]=max(f[i][j],f[i-1][j-1]+1);
}
}
for(i=0;i<26;i++){
for(j=1;j<=la;j++){
if(a[j]=='a'+i) f1[i][j]=j;
else f1[i][j]=f1[i][j-1];
}
for(j=1;j<=lb;j++){
if(b[j]=='a'+i) f2[i][j]=j;
else f2[i][j]=f2[i][j-1];
}
}
int len=f[la][lb];
dfs(la,lb,"",len);
sort(v.begin(),v.end());
for(i=0;i<v.size();i++) cout<<v[i]<<endl;
return 0;
}
[POJ1934] Trip的更多相关文章
- 【题解】POJ1934 Trip (DP+记录方案)
[题解]POJ1934 Trip (DP+记录方案) 题意: 传送门 刚开始我是这么设状态的(谁叫我DP没学好) \(dp(i,j)\)表示钦定选择\(i\)和\(j\)的LCS,然而你会发现这样钦定 ...
- poj1934 Trip【线性DP】【输出方案】
Trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3850 Accepted: 1030 Description ...
- SPOJ33&POJ1934 Trip LCS
题目传送门:https://www.luogu.org/problemnew/show/SP33 题目大意:给出两个字符串,求其LCS(最长公共子序列)的长度与具体方案(相同的串算作同一方案).数据组 ...
- $Poj1934\ Trip$ 线性$DP+$搜索
Luogu Description 爱丽丝和鲍伯想去度假,他们每个人都制定了一个参观城市的清单,该地区正好有26个城市,因此它们被编码为小写字母“a”到“z”.清单上可能重复出现某个城市.因为他们想一 ...
- POJ1934 Trip 题解
LCS 模板,但要输出具体方案,这就很毒瘤了. 神奇的预处理:fa[i][j]表示在 \(a\) 串的前 \(i\) 个字符中,字母表第 \(j\) 个字母最晚出现的位置,fb[i][j]同理. 这样 ...
- 常规DP专题练习
POJ2279 Mr. Young's Picture Permutations 题意 Language:Default Mr. Young's Picture Permutations Time L ...
- 题解 【POJ1934】 Trip
题目意思: 有两个字符串(长度\(<=80\)),按字典序输出它们的最长公共子串的所有情况. 解析 最长公共子序列的长度应该都没问题了吧...有问题请自行百度 但关键是要求出每种情况,还要按字典 ...
- Lesson 4 An existing trip
Text I have just received a letter from my brother,Tim. He is in Australia. He has been there for si ...
- dp or 贪心 --- hdu : Road Trip
Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...
随机推荐
- 如何查看yum安装的程序包都放在哪些地方了?
yum安装, 是先下载下来, 然后安装, 用dnf代替yum后, 配置文件跟yum类似, 也是放在 /etc/dnf/ 目录下的, 也有dnf.conf 配置文件等 dnf list后面还可以跟参数: ...
- 协议:FTP
ylbtech-协议:FTP FTP(File Transfer Protocol,文件传输协议) 是 TCP/IP 协议组中的协议之一.FTP协议包括两个组成部分,其一为FTP服务器,其二为FTP客 ...
- 【转】Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)
[转]Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128) ...
- Vue知识整理2:Vue生命周期方法
在vue执行过程中,可以分为beforeCreate.created.BeforeMount.mounted .BeforeUpdate.updated 等常用的方法,如下图所示. 除此之外,通过查 ...
- Oracle数据库文件导出为CSV格式的方法
1 安装PLSQL Developer,并连接Oracle数据库. 2 执行sql语句,将要导出的表格显示出来. select * from table名; 3 如下点击导出查询结果,选择数据格式,即 ...
- c# thread4——lock,死锁,以及monitor关键字
多线程的存在是提高系统效率,挖掘cpu性能的一种手段,那么控制它,能够协同多个线程不发生bug是关键. 首先我们来看一段不安全的多线程代码. public abstract class Calcula ...
- 【ABAP系列】SAP ABAP解析XML的示例程序
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP解析XML的示例 ...
- TensorFlow学习笔记9-深度模型的优化
深度模型的优化 回顾概念: 代价函数时训练集上损失函数的平均: \[J(\theta)=E_{(x,y)\sim \hat{p}_{data}}L(f(x;\theta),y) \tag{1}\] 引 ...
- 信息收集【采集点OWASP CHINA】网址http://www.owasp.org.cn/
以下部分源于 安全家 http://www.anquanjia.net.cn/newsinfo/729480.html 资源虽多,优质却少.不要被信息海迷惑的心智,新人要想入门,除了优质的系统教学资源 ...
- JavaScript FSO属性大全
什么是FSO? FSO 即 File System Object 文件系统对象,是一种列表 Windows 磁盘目录和文件,对目录和文件进行删除.新建.复制.剪切.移动等操作的技术.使用 FSO 网站 ...