题意:

求最长公共子序列,并且这个子序列是字典序最小。

思路:

LCS附一个值,dp[i][j].first代表以i,j的LCS的长度,dp[i][j].second代表LCS结尾元素,然后利用路径输出就好了;

竖着传就是1,横着就是-1,斜对角就是0;

然后就wa掉了。。。因为单一的判断结尾肯定不行啊,要判断这个满足的串;

138885777

15738

所以second为整个串;所以不需要路径记录了;

直接输出dp[L1][L2].second就好了;

#include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int N=1e2+10;
const int INF=0x3f3f3f3f; int len1,len2;
char s1[N],s2[N];
struct asd{
int first;
string second;
};
asd dp[N][N]; void LCS()
{
for(int i=0;i<=len1;i++)
for(int j=0;j<=len2;j++)
{
dp[i][j].first=0;
dp[i][j].second="";
}
for(int i=1;i<=len1;i++)
{
for(int j=1;j<=len2;j++)
{
if(s1[i]==s2[j])
{
dp[i][j].first=dp[i-1][j-1].first+1;
dp[i][j].second=dp[i-1][j-1].second;
dp[i][j].second+=s1[i];
}
else
{
if(dp[i-1][j].first>dp[i][j-1].first)
{
dp[i][j].first=dp[i-1][j].first;
dp[i][j].second=dp[i-1][j].second;
}
else if(dp[i-1][j].first<dp[i][j-1].first)
{
dp[i][j].first=dp[i][j-1].first;
dp[i][j].second=dp[i][j-1].second;
}
else if(dp[i-1][j].first==dp[i][j-1].first)
{
if(dp[i-1][j].second<dp[i][j-1].second)
{
dp[i][j].first=dp[i-1][j].first;
dp[i][j].second=dp[i-1][j].second;
}
else
{
dp[i][j].first=dp[i][j-1].first;
dp[i][j].second=dp[i][j-1].second;
}
}
}
}
}
if(!dp[len1][len2].first)
{
printf(":(\n");
return;
}
cout<<dp[len1][len2].second<<endl;
} int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",s1+1,s2+1);
len1=strlen(s1+1);
len2=strlen(s2+1);
printf("Case %d: ",cas++);
LCS();
}
return 0;
}

lightoj1010【LCS】的更多相关文章

  1. 【线型DP】【LCS】洛谷P4303 [AHOI2006]基因匹配

    P4303 [AHOI2006]基因匹配 标签(空格分隔): 考试题 nt题 LCS优化 [题目] 卡卡昨天晚上做梦梦见他和可可来到了另外一个星球,这个星球上生物的DNA序列由无数种碱基排列而成(地球 ...

  2. POJ 2250 Compromise【LCS】+输出路径

    题目链接:https://vjudge.net/problem/POJ-2250 题目大意:给出n组case,每组case由两部分组成,分别包含若干个单词,都以“#”当结束标志,要求输出最长子序列. ...

  3. poj 1159 Palindrome 【LCS】

    任意门:http://poj.org/problem?id=1159 解题思路: LCS + 滚动数组 AC code: #include <cstdio> #include <io ...

  4. hdoj 1159 Common Subsequence【LCS】【DP】

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. 【线型DP】【LCS】UVA_10635 Prince and Princess

    嘤嘤嘤,我又来了,刚A完就写,这个沙雕题有丶恶心.                  ???时间4.11发现所有表情包都莫得了 题目: In an n×n chessboard, Prince and ...

  6. poj 1458 Common Subsequence【LCS】

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: 17 ...

  7. HDU1069_Monkey and Banana【LCS】

    Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. HDU 1503【LCS】(字符串合并输出)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 题目大意: 给两个字符串,组成一个长度尽可能小的字符串,它包含上述两个字符串,且原字符串中的字符 ...

  9. 51Nod-1006【LCS】+【输出路径】模板题

    题目链接:https://vjudge.net/contest/225715#problem/B 转载于>>> 题目大意: 给出两个序列,要求输出它们的最长公共子序列. 解题思路: ...

随机推荐

  1. UITableView基础入门

    新建一个Single View Application 添加一个空类如下: using System; using UIKit; using Foundation; namespace BasicTa ...

  2. EasyRTMP实现对接海康、大华等IPCamera SDK进行RTMP推送直播功能

    本文转自EasyDarwin团队Kim的博客:http://blog.csdn.net/jinlong0603 Demo项目介绍 EasyRTMP Demo代码下载地址https://github.c ...

  3. python中的类的成员变量以及property函数

    1 python类的各种变量 1.1 全局变量 在类外定义的变量. 1.2 类变量 定义在类里面,所有的函数外面的变量.这个变量只有一份,是所有的对象共有的.在类外用“类.”来引用. 1.3 实例变量 ...

  4. mount available

    Mount (computing), the process of making a file system accessible mount (Unix), the utility in Unix- ...

  5. Windows平台,开机自动运行应用

    打开注册表编辑器(Win+R后执行regedit) 进入HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 新建字符串值, ...

  6. HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)

    [题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...

  7. ubuntu 16.04安装Jenkins

    快速安装: sudo wget -q -O - http://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add - sudo ...

  8. shapes

    接口 shape package shape; public abstract interface shape { public abstract void Draw(); public abstra ...

  9. Rime输入工具的修改与编译安装

    作为一个比较“事儿多”的五笔用户,在使用过几乎所有的主流输入工具后,我最终选择了定制性非常高(同时也比较难以上手)的Rime.刚开始是在Windows下使用小狼毫0.9.30版,这个工具在上屏速度.热 ...

  10. Linux - Unix环境高级编程(第三版) 源代码编译(即头文件apue.h如何使用问题)【转】

    本文转载自:http://blog.csdn.net/hadas_wang/article/details/43203795 1. 下载代码:http://www.apuebook.com/code3 ...