[题目链接]

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的更多相关文章

  1. poj 1934(LCS)

    转自:http://www.cppblog.com/varg-vikernes/archive/2010/09/27/127866.html 1)首先按照常规的方法求出最长公共子序列的长度也就是用O( ...

  2. $2019$ 暑期刷题记录1:(算法竞赛DP练习)

    $ 2019 $ 暑期刷题记录: $ POJ~1952~~BUY~LOW, BUY~LOWER: $ (复杂度优化) 题目大意:统计可重序列中最长上升子序列的方案数. 题目很直接的说明了所求为 $ L ...

  3. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  4. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

  5. [转] POJ DP问题

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  6. POJ动态规划题目列表

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  7. DP题目列表/弟屁专题

    声明: 1.这份列表不是我原创的,放到这里便于自己浏览和查找题目. ※最近更新:Poj斜率优化题目 1180,2018,3709 列表一:经典题目题号:容易: 1018, 1050, 1083, 10 ...

  8. 三分 --- POJ 3301 Texas Trip

    Texas Trip Problem's Link:   http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形 ...

  9. [POJ 1041] John's Trip

    [题目链接] http://poj.org/problem?id=1041 [算法] 欧拉回路[代码] #include <algorithm> #include <bitset&g ...

随机推荐

  1. 三星的Knox Warranty Bit原理

    它是如何触发的? 这个值的状态是烧录在主板上,无法刷写修改, 从0到1 不可逆,除非替换硬件 If a non-Knox boot loader or kernel has been installe ...

  2. iproute2常用命令

    #常用命令 ip link show #显示链路 ip addr show #显示地址(或ifconfig) ip route show #显示路由(route -n) ip neigh show # ...

  3. Generics of a Higher Kind

    http://adriaanm.github.io/files/higher.pdf https://www.atlassian.com/blog/archives/scala-types-of-a- ...

  4. http 请求头示例

      POST /3-0/app/account/item HTTP/1.1 Host 10.100.138.32:8046 Content-Type application/json Accept-E ...

  5. php 加密解密函数封装

    算法一: //加密函数 function lock_url($txt,$key='yang') { $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi ...

  6. Eclipse(含STS)安装插件/软件、更新

    安装方式 Eclipse安装插件的三种方式 直接复制安装 离线下载好插件,通常去这个插件的官网去找,就是在线安装的地址?如testng可以去http://beust.com/eclipse即http: ...

  7. TCP中的RST标志(Reset)详解

    在谈RST攻击前,必须先了解TCP:如何通过三次握手建立TCP连接.四次握手怎样把全双工的连接关闭掉.滑动窗口是怎么传输数据的.TCP的flag标志位里RST在哪些情况下出现.下面我会画一些尽量简化的 ...

  8. html第八节课

    导航 1.首先在<head>里面引用一个JQUERY的文件以用来制作鼠标点击动画效果(从网站上下载即可) 1 <script language="javascript&qu ...

  9. 【JavaScript框架封装】自己动手封装一个涵盖JQuery基本功能的框架及核心源码分享(单文件版本)

    整个封装过程及阅读JQuery源码的过程基本上持续了一个月吧,最终实现了一个大概30%的JQuery功能的框架版本,但是里面涉及的知识点也是非常多的,总共的代码加上相关的注释大概在3000行左右吧,但 ...

  10. 使用GitHub代码仓库Repositories上传自己的项目代码

    1.下载客户端github(必须下载,需要该软件所提供的Git shell输入命令来上传项目)下载地址: https://github-windows.s3.amazonaws.com/GitHubS ...