题意:

  给两个水果名,要求他们的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. LR中错误代号为27796的解决方法

    问题:   曾经遇到过一个问题,在一次性能测试过程中,使用http协议的多用户向服务器发送请求.设置了持续时间,出现错误为:27796, Failed to connect to server 'ho ...

  2. Ubuntu虚拟机与Window、Arm的通信

    Ubuntu虚拟机与Window的通信安装有Ubuntu14.04的虚拟机VMware,将虚拟机的网络适配器配置成NAT类型(默认使用VMnet8进行通信),此时将Ubuntu的IP地址设置成与VMn ...

  3. Battle Over Cities (25)(DFS、连通图)

    It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...

  4. Java实现MySQL在线管理

    问题: 1.如何实现在线创建MySQL数据库.表: 2.如何实现在线执行sql语句? 思路: 1.可以通过java.sql.Statement类的executeUpdate(String sql)方法 ...

  5. 十二、mysql sql_mode 简学

    .一般默认情况下sql_mode默认为空,也就是不严格的sql检查 .如果sql_mode为空的情况下,测试: )); //定义一个name字段长度为定长2的tt3表 insert into tt3 ...

  6. Linux内核树的建立-基于ubuntu系统

    刚看 O'REILLY 写的<LINUX 设备驱动程序>时.作者一再强调在编写驱动程序时必须 建立内核树.先前的内核只需要有一套内核头文件就够了,但因为2.6的内核模块吆喝内核源码树中的目 ...

  7. EXTJS 4.2 资料 控件之combo 联动

    写两个数据源: 1.IM_ST_Module.js { success:true, data:[ { ModuleId: '1', ModuleName: '资讯' } , { ModuleId: ' ...

  8. python学习笔记18(UliPad 初体验)

    在windows下安装配置Ulipad 由于UliPad 是由wxPython 开发的,所以,需要先安装wxPython . wxPython下载地址: http://www.wxpython.org ...

  9. 转载:传说中的T检验

    第二周结束:传说中的T检验 小耿2014-01-21 10:58 本文和上一篇笔记一样:语言十分啰嗦.请大家忍耐…… 以前我不懂统计的时候(现在也不懂),只知道数据出来了要做三件事:1,检验一下数据是 ...

  10. 3.8 spring - AbstractBeanDefinition 介绍

    之前,我们已尽完成了xml 文档到 GenericBeanDefinition的转换, 也就是说,到这里,所有的配置都可以在GenericBeanDefinition 的实例中找到对应的配置. Gen ...