题意:

  给两个水果名,要求他们的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. JSON操作,转载

    http://www.cnblogs.com/mcgrady/archive/2013/06/08/3127781.html#_label0

  2. Android模拟神器Genymotion eclipse插件安装问题出解决

    我之前一直是打开eclipse之前直接运行Genymotion模拟器就可以连接到adb了,非常方便,但最近突然想来装个eclipse的Genymotion插件玩玩,安装时居然出错了,于是不折腾好心里不 ...

  3. netty sample

    http://netty.io/wiki/ https://github.com/netty/netty/tree/master/example/src/main/java/io/netty/exam ...

  4. python学习笔记29(python中堆的使用)

    堆(heap):优先队列的一种,使用优先队列能够以任意顺序增加对象,并且能在任意时间(可能在增加对象的同时)找到(也可能是移除)最小元素,比用于列表中min的方法要高效. Python中并没有独立的堆 ...

  5. poj 2777 Count Color(线段树)

    题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  6. eclipse 书签

    虽然eclipse有back to和forward两个功能帮助我们阅读代码,但有时候代码一层一层看下去后,会忘了自己最初的起点. 因此想到了eclipse的书签bookmark功能. 首先,添加书签. ...

  7. NOSQL Mongo入门学习笔记 - MongoDB的安装(一)

    手上的工作不是很忙,所以来学习学习很久就像接触的MongoDb,无奈前段时间工作时间都比较多.记录在这里供以后参考 环境: Centos 7 64位 开始: 1. 在官网下载Mongo : wget  ...

  8. [转载]test后跟je

    今天俺也用OD(OllyDbg)反汇编了个小软件,其中里面有下面两条指令: 没太明白什么意思,google一下,在看雪论坛上发现了一个大虾的解释很详细,记录一下: 1.test a,b 是a与b相与的 ...

  9. call && jmp 指令

    对于jmp指令: (1)jmp short 标号相当于(ip)=(ip)+8位位移 跳转范围是[-128,127](2)jmp near ptr 标号相当于(ip)=(ip)+16位位移 跳转范围是[ ...

  10. C++中的构造函数,拷贝构造函数和赋值运算

    关于C++中的构造函数,拷贝构造函数和赋值运算,以前看过一篇<高质量C++/C编程指南>的文章中介绍的很清楚,网上能搜索到,如果想详细了解这方面的知识可以参看一下这篇文章. 常见的给对象赋 ...