[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 ...
随机推荐
- 大白话理解cookie
HTTP协议是一个无状态的协议,服务器无法区分出两次请求是否发送自同一服务器. 需要通过会话控制来解决这个问题,会话控制主要有两种方式Cookie 和 Session. Cookie就是一个头,Coo ...
- 深入浅出java多态
所谓多态就是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定,即一个引用变量倒底会指向哪个类的实例对象,该引用变量发出的方法调用到底是哪个 ...
- HttpWebRequest 知识点
string Url = System.Configuration.ConfigurationManager.AppSettings["CallPaperInvoiceURL"]; ...
- alert弹出框 弹出窗口 ----sweetAlert
推荐一款好用的alert,下面地址是demo,很直观的看到效果,wap可以使用 http://www.dglives.com/demo/sweetalert-master/example/ < ...
- jquery radio、 checkbox、 select 操作
转载:http://www.haorooms.com/post/checkandselect $("input[id^='code']");//id属性以code开始的所有inpu ...
- [iOS Reverse]logify日志追踪,锁定注入口-控制台查看
前言 logify是theos的一个组件,路径是: /opt/theos/bin/logify.pl 我们还是以微信红包为例子,根据[iOS Hacking]运行时分析cycript得到的入口文件: ...
- Vue.js大总结
最近回顾了一下Vue.js的基础知识,把认为重要的几个点简单的罗列了出来 vue渐进式的理解 vue可以开发很多插件,可以把很多插件组合到一起,渐进的增加vue的功能 update beforeUpd ...
- 51nod1117 聪明的木匠【贪心+优先队列】
一位老木匠需要将一根长的木棒切成N段.每段的长度分别为L1,L2,......,LN(1 <= L1,L2,-,LN <= 1000,且均为整数)个长度单位.我们认为切割时仅在整数点处切且 ...
- HDU1850 - Being a Good Boy in Spring Festival 【尼姆博弈】
一年在外 父母时刻牵挂 春节回家 你能做几天好孩子吗 寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场 悄悄给爸爸买个小礼物 主动地 强烈地 要求洗一次碗 某一天早起 给爸妈用心地做回早餐 ...
- 在centos6上面安装python3.4
mkdir -p /work cd /work gwet https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tar.xz tar -axf Py ...