题意:

  给两个水果名,要求他们的LCS部分只输出1次,其他照常输出,但是必须保持原来的顺序!

思路:

  求LCS是常规的,但是输出麻烦了,要先求LCS,再标记两串中的所有LCS字符,在遇到LCS字符时,先输串1的,再输串2的,然后输该字符,以保证每个LCS字符输出前,两串中该字符前面的字符全部已输出。

 //#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=+;
char s1[N], s2[N]; int mark[N][N];
int cnt[N][N]; int pos1[N];
int pos2[N]; void LCS()
{
memset(mark, , sizeof(mark));
memset(cnt, , sizeof(cnt));
int len1=strlen(s1+);
int len2=strlen(s2+); for(int i=; i<=len1; i++)
{
for(int j=; j<=len2; j++)
{
if(s1[i]==s2[j])
{
cnt[i][j]=cnt[i-][j-]+;
mark[i][j]=; //斜的
}
else if(cnt[i-][j]>=cnt[i][j-])
{
cnt[i][j]=cnt[i-][j];
mark[i][j]=; //上边
}
else
{
cnt[i][j]=cnt[i][j-];
mark[i][j]=; //左边
}
}
}
int t1=len1, t2=len2;
memset(pos1,,sizeof(pos1));
memset(pos2,,sizeof(pos2));
while(t1> && t2>)
{
int pre=mark[t1][t2]; if(pre==)
{
pos1[t1]=;
pos2[t2]=;
t1--,t2--;
}
else if(pre==) t1--;
else t2--;
}
int i=, j=; while()
{
while(i<=len1 && !pos1[i])
printf("%c",s1[i++]);
i++;
while(j<=len2 && !pos2[j])
printf("%c",s2[j++]);
if(j<=len2) printf("%c",s2[j]);
j++;
if(i>len1&&j>len2) return ;
}
} int main()
{
freopen("input.txt", "r", stdin);
while(~scanf("%s%s", s1+, s2+))
{
LCS();
printf("\n");
}
return ;
}

AC代码

HDU 1503 Advanced Fruits (LCS,变形)的更多相关文章

  1. hdu 1503 Advanced Fruits(LCS输出路径)

    Problem Description The company "21st Century Fruits" has specialized in creating new sort ...

  2. hdu 1503:Advanced Fruits(动态规划 DP & 最长公共子序列(LCS)问题升级版)

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

  3. hdu 1503 Advanced Fruits(最长公共子序列)

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

  4. 最长公共子序列(加强版) Hdu 1503 Advanced Fruits

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

  5. hdu 1503 Advanced Fruits 最长公共子序列 *

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

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

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

  7. 题解报告:hdu 1503 Advanced Fruits(LCS加强版)

    Problem Description The company "21st Century Fruits" has specialized in creating new sort ...

  8. HDU 1503 Advanced Fruits (LCS+DP+递归)

    题意:给定两个字符串,让你求一个最短的字符串,并且这个字符串包含给定的两个. 析:看到这个题,我知道是DP,但是,不会啊...完全没有思路么,我就是个DP渣渣,一直不会做DP. 最后还是参考了一下题解 ...

  9. hdu 1503 Advanced Fruits

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 思路:这是一道最长公共子序列的题目,当然还需要记录路径.把两个字符串的最长公共字串记录下来,在递 ...

随机推荐

  1. Linux中Kill进程的N种方法

    常规篇: 首先,用ps查看进程,方法如下: $ ps -ef …… smx       1822     1  0 11:38 ?        00:00:49 gnome-terminal smx ...

  2. ExtJS 添加图标icon

    extjs控件有两个属性:一个是iconCls:另一个是icon.通过这两个属性可以对控件添加图标 1.直接引用图标路径 icon: '../icons/application_view_detail ...

  3. Spring execution 表达式

    execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) th ...

  4. python学习笔记25(文件管理 os包)

    os包我们经常会与文件和目录打交道,对于这些操作python提供了一个os模块,里面包含了很多操作文件和目录的函数.如果你对linux基本操作了解的话,下面的一些os方法应该会很熟悉的,因为基本和li ...

  5. c#类库中使用Session

    网站开发中,为了保存用户的信息,有时候需要使用session.如果我们在aspx页面中使用Session,只需要Session["key"]=value就可以,获取时使用int u ...

  6. bnuoj 27874 "Center" of [p]erimeter midpoints(计算几何)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=27874 [题意]: 给你一个三角形三个顶点的坐标ABC,三角形各边取一点DEF,将三角形周长平均分割 ...

  7. submit和button提交表单的区别

    <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  8. ExtJS4.2学习(20)动态数据表格之前几章总结篇1(转)

    鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2014-02-18/196.html --------------- ...

  9. uva 108

    降维  枚举行累加   然后求单行上最大连续和 #include <iostream> #include <cstring> #include <cstdio> # ...

  10. [BEC][hujiang] Lesson03 Unit1:Working life ---Grammar & Listening & Vocabulary

    3 Working life p8 Grammar Gerund and infinitive(动名词和不定式) 一般而言:        1 动词后面接动名词还是不定式没有特定规则,主要取决于语言习 ...