Advanced Fruits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3340    Accepted Submission(s): 1714
Special Judge

Problem Description
The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit into the genome of another one. Most times this method doesn't work, but sometimes, in very rare cases, a new fruit emerges that tastes like a mixture between both of them. 
A big topic of discussion inside the company is "How should the new creations be called?" A mixture between an apple and a pear could be called an apple-pear, of course, but this doesn't sound very interesting. The boss finally decides to use the shortest string that contains both names of the original fruits as sub-strings as the new name. For instance, "applear" contains "apple" and "pear" (APPLEar and apPlEAR), and there is no shorter string that has the same property.

A combination of a cranberry and a boysenberry would therefore be called a "boysecranberry" or a "craboysenberry", for example.

Your job is to write a program that computes such a shortest name for a combination of two given fruits. Your algorithm should be efficient, otherwise it is unlikely that it will execute in the alloted time for long fruit names.

 
Input
Each line of the input contains two strings that represent the names of the fruits that should be combined. All names have a maximum length of 100 and only consist of alphabetic characters.

Input is terminated by end of file.

 
Output
For each test case, output the shortest name of the resulting fruit on one line. If more than one shortest name is possible, any one is acceptable.
 
Sample Input
apple peach
ananas banana
pear peach
 
Sample Output
appleach
bananas
pearch
 
 
这道题就是给你两个单词,然后你要把两个单词拼接成一个新单词,使得新单词的子序列中包含两个单词,并且要使这个新单词最短。所以这道题就是求最长公共子序列,并且要记录下子序列的字母,以及他们在主串和副串中的原始位置,之后进行拼接输出。
 #include<stdio.h>
#include<string.h> const int maxn=; char str1[maxn],str2[maxn];
int dp[maxn][maxn],len; //len指示ans的长度 struct node{
int i,j; //i记录主串位置,j记录副串当前字符位置
char ch; //记录当前字符
}ans[maxn]; int max(int a,int b){
return a>b?a:b;
} void LCS(int m,int n){
memset(dp,,sizeof(dp));
int i,j;
for(i=;i<=m;i++)
for(j=;j<=n;j++)
if(str1[i]==str2[j])
dp[i][j]=dp[i-][j-]+;
else
dp[i][j]=max(dp[i-][j],dp[i][j-]);
if(dp[m][n]==){ //如果没有公共序列,直接输出
printf("%s%s",str1,str2);
}else{
i=m;j=n;
len=;
while(i!= && j!=){ //取出最长公共子序列的字母
if((dp[i][j]==dp[i-][j-]+) && str1[i]==str2[j]){
ans[len].i=i;
ans[len].j=j;
ans[len++].ch=str1[i]; //倒序保存最长公共子序列字母
i--;j--;
}else if(dp[i-][j]>dp[i][j-])
i--;
else
j--;
}
}
} int main(){ //freopen("input.txt","r",stdin); int len1,len2,i,j,k;
while(scanf("%s%s",str1+,str2+)!=EOF){
len1=strlen(str1+);
len2=strlen(str2+);
LCS(len1,len2);
i=j=;
for(k=len-;k>=;k--){
while(i!=ans[k].i){
printf("%c",str1[i]);
i++;
}
while(j!=ans[k].j){
printf("%c",str2[j]);
j++;
}
printf("%c",ans[k].ch);
i++;j++;
}
printf("%s%s\n",str1++ans[].i,str2++ans[].j);
}
return ;
}

根据LCS的原理,将每个字符都进行标记,看两个字符串中对应的字符究竟处于什么状态,然后输出,其标记为公共子串的字符只输出一次即可

 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; char s1[],s2[];
int len1,len2,dp[][],mark[][]; void LCS()
{
int i,j;
memset(dp,,sizeof(dp));
for(i = ;i<=len1;i++)
mark[i][] = ;
for(i = ;i<=len2;i++)
mark[][i] = -;
for(i = ; i<=len1; i++)
{
for(j = ; j<=len2; j++)
{
if(s1[i-]==s2[j-])
{
dp[i][j] = dp[i-][j-]+;
mark[i][j] = ;
}
else if(dp[i-][j]>=dp[i][j-])
{
dp[i][j] = dp[i-][j];
mark[i][j] = ;
}
else
{
dp[i][j] = dp[i][j-];
mark[i][j] = -;
}
}
}
} void PrintLCS(int i,int j)
{
if(!i && !j)
return ;
if(mark[i][j]==)
{
PrintLCS(i-,j-);
printf("%c",s1[i-]);
}
else if(mark[i][j]==)//根据回溯的位置进行输出
{
PrintLCS(i-,j);
printf("%c",s1[i-]);
}
else
{
PrintLCS(i,j-);
printf("%c",s2[j-]);
}
} int main()
{
while(~scanf("%s%s",s1,s2))
{
len1 = strlen(s1);
len2 = strlen(s2);
LCS();
PrintLCS(len1,len2);
printf("\n");
} return ;
}

hdu 1503 Advanced Fruits(最长公共子序列)的更多相关文章

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

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

  2. HDU 1159 Common Subsequence 最长公共子序列

    HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...

  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

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

  5. HDU 1513 Palindrome(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 解题报告:给定一个长度为n的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个 ...

  6. hdu 1159 Common Subsequence(最长公共子序列 DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

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

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

  8. HDU 4681 string 求最长公共子序列的简单DP+暴力枚举

    先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...

  9. hdu 1080 dp(最长公共子序列变形)

    题意: 输入俩个字符串,怎样变换使其所有字符对和最大.(字符只有'A','C','G','T','-') 其中每对字符对应的值如下: 怎样配使和最大呢. 比如: A G T G A T G -  G ...

随机推荐

  1. Linux时间时区详解与常用时间函数

    时间与时区 整个地球分为二十四时区,每个时区都有自己的本地时间. Ø  UTC时间 与 GMT时间 我们可以认为格林威治时间就是时间协调时间(GMT = UTC),格林威治时间和UTC时间都用秒数来计 ...

  2. linux下apache php配置redis

    1.安装redis 第一步: 下载:https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz 上传phpredis-2.2.4.tar.gz ...

  3. EL 表达式 函数 操作 字符串

    <%@tablib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> ${fn ...

  4. sql server 2008 去除html标签

    由于商品详情数据库的字段是text,存放的是html,但是要求导出的商品详情中只是商品的描述,不要标签,原来打算先把数据导入excel中,然后利用java的正则去替换,结果由于商品详情太大,一个单元格 ...

  5. wcf利用IDispatchMessageInspector实现接口监控日志记录和并发限流

    一般对于提供出来的接口,虽然知道在哪些业务场景下才会被调用,但是不知道什么时候被调用.调用的频率.接口性能,当出现问题的时候也不容易重现请求:为了追踪这些内容就需要把每次接口的调用信息给完整的记录下来 ...

  6. Android笔记之使用Glide加载网络图片、下载图片

    Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...

  7. 中国移动OneNet平台上传GPS数据JSON格式

    最终目的输出 POST /devices/3225187/datapoints HTTP/1.1 api-key: R9xO5NZm6oVI4YBHvCPKEqtwYtMA Host: api.hec ...

  8. STM32L0 HAL库 TIM定时1s

    STM32L0的定制器资源: 本实验使用TIM6 HSI频率是16Mhz,则单指令周期是1/16Mhz 预分频设置为1600,则每跑1600下,定时器加1,相当于定时器加1的时间是1600*(1/16 ...

  9. mysql 中 all any some 用法

    -- 建表语句 CREATE TABLE score( id INT PRIMARY KEY AUTO_INCREMENT, NAME ), SUBJECT ), score INT); -- 添加数 ...

  10. Microsoft Office Document Imaging批量ocr 方法

    先将pdf文件->导出->tiff文件,生成pdf每页的tiff文件 使用 G:\SoftWare-new\tiff文件合并拆分工具 将一个导出的单个tiff合并为一个tiff文件 再用 ...