poj 1934(LCS)
转自:http://www.cppblog.com/varg-vikernes/archive/2010/09/27/127866.html
1)首先按照常规的方法求出最长公共子序列的长度
也就是用O(MN)的那个动态规划,结果放在二维数组dp里
dp[i][j] = { 字串a的1~i部分与字串b的1~j部分的最长公共子序列的长度 }
2)求辅助数组
last1[i][j] = { 到下标i为止,字符j在字串a中最后一次出现的下标 }
last2[i][j] = { 到下标i为止,字符j在字串b中最后一次出现的下标 }
3)枚举最长公共字串的每一个字符
从最后一个字符开始枚举
比如说现在枚举最后一个字符是'C'的情况。
那么 'CDCD' 与 'FUCKC' 这两个字串。
一共有 (0, 2) (0, 4) (2, 2) (2. 4) 这四种可能。
很明显前三个是可以舍弃的,因为第四个优于前三个,为后续的枚举提供了更大的空间。
last数组正好是用来做这个的。
4)排序输出
代码里用了stl的set。
// File Name: 1934.cpp
// Author: Missa_Chen
// Created Time: 2013年07月07日 星期日 20时21分33秒 #include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <vector>
#include <time.h> using namespace std;
const int maxn = ;
int dp[maxn][maxn];
int last1[maxn][], last2[maxn][];
set <string> ans;
char tmp[maxn];
void dfs(int s1, int s2, int len)
{
if (len <= )
{
ans.insert(tmp);
return ;
}
if (s1 > && s2 > )
{
for (int i = ; i < ; ++i)
{
int t1 = last1[s1][i];
int t2 = last2[s2][i];
if (dp[t1][t2] == len)
{
tmp[len - ] = 'a' + i;
dfs(t1 - , t2 - , len - );
}
}
}
return ;
}
void LCS(string s1, string s2)
{
memset(dp, , sizeof(dp));
for (int i = ; i <= s1.size(); ++i)
{
for (int j = ; j <= s2.size(); ++j)
{
if (s1[i - ] == s2[j - ])
dp[i][j] = dp[i - ][j - ] + ;
else dp[i][j] = max(dp[i - ][j], dp[i][j - ]);
}
}
}
void solve(string s1, string s2)
{
memset(last1, , sizeof(last1));
memset(last2, , sizeof(last2));
for (int i = ; i <= s1.size(); ++i)
{
for (int j = ; j < ; ++j)
last1[i][j] = last1[i - ][j];
last1[i][s1[i - ] - 'a'] = i;
}
for (int i = ; i <= s2.size(); ++i)
{
for (int j = ; j < ; ++j)
last2[i][j] = last2[i - ][j];
last2[i][s2[i - ] - 'a'] = i;
}
tmp[dp[s1.size()][s2.size()]] = '\0';
dfs(s1.size(), s2.size(), dp[s1.size()][s2.size()]);
for (set <string> :: iterator it = ans.begin(); it != ans.end(); ++it)
cout <<*it<<endl;
}
int main()
{
string s1, s2;
while (cin >> s1 >> s2)
{
LCS(s1, s2);
solve(s1, s2);
}
return ;
}
poj 1934(LCS)的更多相关文章
- [POJ 1934] Trip
[题目链接] http://poj.org/problem?id=1934 [算法] 先用dp求出LCS,然后搜索即可,注意加上一些剪枝 [代码] #include <algorithm> ...
- POJ 2250(LCS最长公共子序列)
compromise Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- poj 2264(LCS)
Advanced Fruits Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2158 Accepted: 1066 ...
- POJ 2217 LCS(后缀数组)
Secretary Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1655 Accepted: 671 Descript ...
- $2019$ 暑期刷题记录1:(算法竞赛DP练习)
$ 2019 $ 暑期刷题记录: $ POJ~1952~~BUY~LOW, BUY~LOWER: $ (复杂度优化) 题目大意:统计可重序列中最长上升子序列的方案数. 题目很直接的说明了所求为 $ L ...
- POJ 1159 回文串-LCS
题目链接:http://poj.org/problem?id=1159 题意:给定一个长度为N的字符串.问你最少要添加多少个字符才能使它变成回文串. 思路:最少要添加的字符个数=原串长度-原串最长回文 ...
- LCS POJ 1458 Common Subsequence
题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...
- hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)
题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...
- POJ 2250 Compromise(LCS)
POJ 2250 Compromise(LCS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#proble ...
随机推荐
- Apache CXF实现Web Service(5)—— GZIP使用
Apache CXF实现Web Service(5)-- GZIP使用 参考来源: CXF WebService整合Spring Apache CXF实现Web Service(1)--不借助重量级W ...
- 高德地图根据经纬度转换成地址JS代码demo
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 你必须知道的ADO.NET
原文:http://www.cnblogs.com/liuhaorain/archive/2012/02/06/2340409.html 1. 什么是ADO.NET? 简单的讲,ADO.NET是一组允 ...
- 2016年度 JavaScript 展望(上)
[编者按]本文作者为资深 Web 开发者 TJ VanToll, TJ 专注于移动端 Web 应用及其性能,是<jQuery UI 实践> 一书的作者. 本文系 OneAPM 工程师编译呈 ...
- JQuery原理及深入解析--转载
总体架构 jQuery是个出色的javascript库,最近结合它写javascript,看了下源码. 先从整体.全局的看,jQuery的源码几乎都在下面的代码中: (function() { //… ...
- Linux性能检测命令 - vmstat
一.vmstat命令描述 最常见的Linux/Unix监控工具想必是vmstat了,vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可以展现给定时间间隔的服务 ...
- editplus bat语法高亮
editplus bat语法高亮 今天需要在Windows上写批处理,因为没写过,避免关键字错误,就需要语法高亮了,editplus默认没有bat语法文件,赶紧解决. 1:到 http://www.e ...
- 被 Windows 10 SDK 坑了
抓包抓到的配置文件与完整版的VS一致,虽然显示出来的只有 Windows 10 通用工具,我还以为是只更新这些(因为列出来的几个,我确实之前没装),就愉快地点了安装,结果……新建项目时,发现 Wind ...
- ArrayList,LinkedList,Vector,Stack之间的区别
一,线程安全性 Vector.Stack:线程安全 ArrayList.LinkedList:非线程安全 二,实现方式 LinkedList:双向链表 ArrayList,Vector,Stack:数 ...
- 怎么修改mysql密码
1.用root 进入mysql后mysql>set password =password('你的密码');mysql>flush privileges; 2.使用GRANT语句 mysql ...