[POJ 1934] Trip
[题目链接]
http://poj.org/problem?id=1934
[算法]
先用dp求出LCS,然后搜索即可,注意加上一些剪枝
[代码]
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXLEN 85 int i,j,la,lb,len,l;
string a,b;
int f[MAXLEN][MAXLEN],pa[MAXLEN][MAXLEN],pb[MAXLEN][MAXLEN];
string q[]; inline void dfs(int dep,int pos1,int pos2,string t)
{
int i;
if (dep > len) q[++l] = t;
if (pos1 < || pos2 < ) return;
if (f[pos1][pos2] != len - dep + ) return;
if (dep + pos1 < len || dep + pos2 < len) return;
if (l >= ) return;
for (i = ; i < ; i++)
{
if (l >= ) return;
if (pa[i][pos1] != - && pb[i][pos2] != -)
dfs(dep + ,pa[i][pos1] - ,pb[i][pos2] - ,(char)(i + 'a') + t);
}
} int main()
{ cin.tie();
ios :: sync_with_stdio();
while (cin >> a)
{
cin >> b;
la = a.size();
lb = b.size();
for (i = ; i < la; i++)
{
for (j = ; j < lb; j++)
{
f[i][j] = ;
}
}
for (i = ; i < la; i++)
{
for (j = ; j < lb; j++)
{
if (a[i] == b[j])
{
if (i >= && j >= ) f[i][j] = f[i - ][j - ] + ;
else f[i][j] = (a[i] == b[j]);
}
if (i >= ) f[i][j] = max(f[i][j],f[i - ][j]);
if (j >= ) f[i][j] = max(f[i][j],f[i][j - ]);
}
}
for (i = ; i < ; i++)
{
for (j = ; j < la; j++)
{
pa[i][j] = -;
}
}
for (i = ; i < ; i++)
{
for (j = ; j < la; j++)
{
if (a[j] == 'a' + i) pa[i][j] = j;
else if (j > ) pa[i][j] = pa[i][j - ];
}
}
for (i = ; i < ; i++)
{
for (j = ; j < lb; j++)
{
pb[i][j] = -;
}
}
for (i = ; i < ; i++)
{
for (j = ; j < lb; j++)
{
if (b[j] == 'a' + i) pb[i][j] = j;
else if (j > ) pb[i][j] = pb[i][j - ];
}
}
len = f[la - ][lb - ];
l = ;
dfs(,la - ,lb - ,"");
sort(q+,q+l+);
for (i = ; i <= l; i++) cout<< q[i] << endl;
} return ; }
[POJ 1934] Trip的更多相关文章
- poj 1934(LCS)
转自:http://www.cppblog.com/varg-vikernes/archive/2010/09/27/127866.html 1)首先按照常规的方法求出最长公共子序列的长度也就是用O( ...
- $2019$ 暑期刷题记录1:(算法竞赛DP练习)
$ 2019 $ 暑期刷题记录: $ POJ~1952~~BUY~LOW, BUY~LOWER: $ (复杂度优化) 题目大意:统计可重序列中最长上升子序列的方案数. 题目很直接的说明了所求为 $ L ...
- poj动态规划列表
[1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...
- POJ 动态规划题目列表
]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...
- [转] POJ DP问题
列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...
- POJ动态规划题目列表
列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...
- DP题目列表/弟屁专题
声明: 1.这份列表不是我原创的,放到这里便于自己浏览和查找题目. ※最近更新:Poj斜率优化题目 1180,2018,3709 列表一:经典题目题号:容易: 1018, 1050, 1083, 10 ...
- 三分 --- POJ 3301 Texas Trip
Texas Trip Problem's Link: http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形 ...
- [POJ 1041] John's Trip
[题目链接] http://poj.org/problem?id=1041 [算法] 欧拉回路[代码] #include <algorithm> #include <bitset&g ...
随机推荐
- (转载) ORA-12537:TNS连接已关闭
今天在远程客户端配置EBS数据库连接的时候发生“ORA-12537:TNS连接已关闭”的错误.进入服务器运行如下命令:$tnsping VIS 这里VIS如果定义服务名,可以写成 $ tnsping ...
- halcon 模板匹配 -- find_shape_model
find_shape_model(Image : : //搜索图像 ModelID, //模板句柄 AngleStart, // 搜索时的起始角度 AngleExtent, //搜索时的角度范围, ...
- 分布式机器学习框架:CXXNet
caffe是很优秀的dl平台.影响了后面很多相关框架. cxxnet借鉴了很多caffe的思想.相比之下,cxxnet在实现上更加干净,例如依赖很少,通过mshadow的模板化使得gpu ...
- Scala: Types of a higher kind
One of the more powerful features Scala has is the ability to generically abstract across things tha ...
- wget 批量下载网站目录下的文件
执行如下命令就会自动下载 http://www.iyunwei.com/docs/ 下面的所有文件: wget -nd -r -l1 --no-parent http://www.iyunwei.co ...
- day001 Python 计算机基础(2019年5月16日)
      作为一名程序员或者即将踏入IT行业的准程序员,学习任何一门编程语言,都需要有基本的计算机基础 ...
- BZOJ : [Usaco2013 Nov]Crowded 单调队列
正反两遍个来一次单调队列 DP 即可. Code: #include<cstdio> #include<deque> #include<algorithm> usi ...
- PAT_A1109#Group Photo
Source: PAT A1109 Group Photo (25 分) Description: Formation is very important when taking a group ph ...
- 单点登录之 CAS SSO 从入门到精通(第一天)
转自:http://blog.csdn.net/lifetragedy/article/details/43817903 啊......it's quite a long time. 好久没更新博客了 ...
- mapbox 支持国家2000 坐标系的数据
最近有个项目使用mapbox作为地图展示,但是mapbox 只支持web 墨卡托(3857)坐标系的数据.而客户想接入其他单位发布的共享数据,共享数据都是基于arcgisserver 发布的国家200 ...