[题目链接]

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 ; }

最新文章

  1. Python学习--05函数
  2. SFTP交互式文件传输
  3. C#委托全解析
  4. 分模块的maven项目调试时报Source not found的解决办法
  5. HTML5中的音视频处理
  6. 第七章 美化DetailView界面
  7. ZOJ 3903 Ant ZOJ Monthly, October 2015 - A
  8. BZOJ3806: Neerc2011 Dictionary Size
  9. Android图片框架---Glide
  10. [转] 条件变量(Condition Variable)详解
  11. java中Class.forName与new
  12. hdu4267 A Simple Problem with Integers
  13. Java之线程的控制
  14. [置顶] Guava学习之Immutable集合
  15. 线性表 linear_list 顺序存储结构
  16. 数据结构系列(4)之 B 树
  17. 使用jquery移除前面通过onclick绑定的元素的事件,然后重新绑定别的函数来执行onclick事件。
  18. React_生命周期
  19. springCloud、springBoot学习
  20. netty4.0 Server和Client的通信

热门文章

  1. iphone(苹果)手机浏览器顶部下拉出现网页源
  2. 运用&lt;body&gt;属性,渲染页面效果
  3. 关于VirtualBox与锐捷冲突导致锐捷不断掉线的问题的解决办法
  4. 【Oracle】解决oracle sqlplus 中上下左右backspace不能用
  5. c# ado.net eftity framework 返回多表查询结果
  6. JavaOO小结二,及MySQL小结
  7. asp.net mvc学习入门
  8. Linux 之WinSCP连接FTP
  9. 通过yum仓库安装mysql
  10. HDU1069 - Monkey and Banana【dp】