还想用hash记录……果然是天真。lcs转移比较简单,每次增加1。每次找是当前-1的就行了。

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; const int mod = ;
const int maxn = ;
char a[maxn];
char b[maxn];
int dp[maxn][maxn];
int fa[mod];
int na, nb; int main() {
// freopen("in", "r", stdin);
while(~scanf("%s %s", a, b)) {
memset(dp, , sizeof(dp));
memset(fa, -, sizeof(fa));
na = strlen(a);
nb = strlen(b);
for(int i = ; i < na; i++) {
for(int j = ; j < nb; j++) {
if(a[i] == b[j]) {
if(dp[i][j] + > max(dp[i][j+], dp[i+][j])) {
dp[i+][j+] = dp[i][j] + ;
// int cur = (((i + 1) * 59) % mod + ((j + 1) * 61) % mod) % mod;
// int pre = ((i * 59) % mod + (j * 61) % mod) % mod;
// fa[cur][0] = pre;
// fa[cur][1] = i;
// ii = cur;
}
else dp[i+][j+] = max(dp[i][j+], dp[i+][j]);
}
else {
dp[i+][j+] = max(dp[i][j+], dp[i+][j]);
}
}
}
int ii = na;
int jj = nb;
char st[maxn];
int top = ;
while(dp[ii][jj]) {
if(dp[ii][jj] == dp[ii-][jj]) ii--;
else if(dp[ii][jj] == dp[ii][jj-]) jj--;
else {
ii--, jj--;
st[top++] = a[ii];
} }
while(top) printf("%c", st[--top]);
printf("\n");
}
return ;
}

LCS记录路径的更多相关文章

  1. HDU 1503 Advanced Fruits(LCS+记录路径)

    http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是 ...

  2. HDU1503(LCS,记录路径)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. F - LCS 题解(最长公共子序列记录路径)

    题目链接 题目大意 给你两个字符串,任意写出一个最长公共子序列 字符串长度小于3e3 题目思路 就是一个记录路径有一点要注意 找了好久的bug 不能直接\(dp[i][j]=dp[i-1][j-1]+ ...

  4. Educational DP Contest F - LCS (LCS输出路径)

    题意:有两个字符串,求他们的最长公共子序列并输出. 题解:首先跑个LCS记录一下dp数组,然后根据dp数组来反着还原路径,只有当两个位置的字符相同时才输出. 代码: char s[N],t[N]; i ...

  5. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  6. POJ 3436:ACM Computer Factory(最大流记录路径)

    http://poj.org/problem?id=3436 题意:题意很难懂.给出P N.接下来N行代表N个机器,每一行有2*P+1个数字 第一个数代表容量,第2~P+1个数代表输入,第P+2到2* ...

  7. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

  8. hdu1074 状压DP、栈实现记录路径

    题意:给了几门学科作业.它们的截止提交期限(天数).它们的需要完成的时间(天数),每项作业在截止日期后每拖延一天扣一学分,算最少扣的学分和其完成顺序. 一开始做的时候,只是听说过状态压缩这个神奇的东西 ...

  9. hdu 1074(状态压缩dp+记录路径)

    题意:给了n个家庭作业,然后给了每个家庭作业的完成期限和花费的实践,如果完成时间超过了期限,那么就要扣除分数,然后让你找出一个最优方案使扣除的分数最少,当存在多种方案时,输出字典序最小的那种,因为题意 ...

随机推荐

  1. Apache环境.htaccess伪静态301跳转(www与不带www)

    一般而言,我们使用的301跳转就是WWW与不带WWW域名之间的跳转,用行话说就是权重要归一.对于我们使用Apache环境的来说其实相对nginx比较简单,因为只需要我们在网站根目录有.htaccess ...

  2. [geeksforgeeks] Count the number of occurrences in a sorted array

    Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a ...

  3. poj 3254

    状态压缩 dp dp[i][j] 为第 i 行状态为 j 的总数 #include <cstdio> #include <cstdlib> #include <cmath ...

  4. 充分发挥异步在 ASP.NET 中的强大优势

    作者:Brij Bhushan Mishra 最近几年,异步编程受到极大关注,主要是出于两个关键原因:首先,它有助于提供更好的用户体验,因为不会阻塞 UI 线程,避免了处理结束前出现 UI 界面挂起. ...

  5. 7 天玩转 ASP.NET MVC — 第 7 天

    目录 第 1 天 第 2 天 第 3 天 第 4 天 第 5 天 第 6 天 第 7 天 0. 前言 今天是开心的一天.因为我们终于来到了系列学习的最后一节.我相信你喜欢之前的课程,并从中学到了许多. ...

  6. tomcat 常见错误

    1.启动时加载会话错误 Exception loading sessions from persistent storage 处理方法:删除tomcat对应的项目目录下 work/session.se ...

  7. hdu 3658 How many words

    思路: 构造矩阵,矩阵快速幂!!! 代码如下: #include<cstdio> #include<vector> #include<cmath> #include ...

  8. Linux zip解压/压缩并指定目录

    方法如下: 压缩并指定目录举例:zip -r /home/kms/kms.zip /home/kms/server/kms 解压并指定目录 举例:unzip /home/kms/kms.zip -d ...

  9. SVN使用之分支、合并

    首先说下为什么我们需要用到分支-合 并.比如项目demo下有两个小组,svn下有一个trunk版.由于客户需求突然变化,导致项目需要做较大改动,此时项目组决定由小组1继续完成原来正 进行到一半的工作[ ...

  10. DP+矩阵快速幂 HDOJ 5318 The Goddess Of The Moon

    题目传送门 /* DP::dp[i][k] 表示选择i个字符串,最后一次是k类型的字符串,它由sum (dp[i-1][j]) (a[j], a[k] is ok)累加而来 矩阵快速幂:将n个字符串看 ...