hdu 1503 Advanced Fruits(最长公共子序列)
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
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 is terminated by end of file.
ananas banana
pear peach
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(最长公共子序列)的更多相关文章
- hdu 1503 Advanced Fruits 最长公共子序列 *
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1159 Common Subsequence 最长公共子序列
HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...
- 最长公共子序列(加强版) Hdu 1503 Advanced Fruits
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1503 Advanced Fruits
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 思路:这是一道最长公共子序列的题目,当然还需要记录路径.把两个字符串的最长公共字串记录下来,在递 ...
- HDU 1513 Palindrome(最长公共子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 解题报告:给定一个长度为n的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个 ...
- hdu 1159 Common Subsequence(最长公共子序列 DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- 题解报告:hdu 1503 Advanced Fruits(LCS加强版)
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
- HDU 4681 string 求最长公共子序列的简单DP+暴力枚举
先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...
- hdu 1080 dp(最长公共子序列变形)
题意: 输入俩个字符串,怎样变换使其所有字符对和最大.(字符只有'A','C','G','T','-') 其中每对字符对应的值如下: 怎样配使和最大呢. 比如: A G T G A T G - G ...
随机推荐
- (1)安装kvm
我的环境是redhat虚拟机,版本信息如下: [root@localhost ~]# cat /etc/issue Red Hat Enterprise Linux Server release 6. ...
- PHP性能之语言性能优化:vld——查看代码opcode的神器
vld介绍 vld是PECL(PHP 扩展和应用仓库)的一个PHP扩展,现在最新版本是 0.14.0(2016-12-18),它的作用是:显示转储PHP脚本(opcode)的内部表示(来自PECL的v ...
- Java研发工程师面试题
基础题 一.String,StringBuffer, StringBuilder 的区别是什么?String为什么是不可变的?1. String是字符串常量,StringBuffer和StringBu ...
- ios8 一些运行问题
iOS10相册相机闪退bughttp://www.jianshu.com/p/5085430b029fiOS 10 因苹果健康导致闪退 crashhttp://www.jianshu.com/p/5 ...
- web安全之SQL注入---第四章 如何进行SQL注入攻击
第四章 如何进行SQL注入攻击1.数字注入2.字符串注入 '# '--
- R语言数据分析系列之四
R语言数据分析系列之四 -- by comaple.zhang 说到统计分析我们就离不开随机变量,所谓随机变量就是数学家们为了更好的拟合现实世界的数据而建立的数学模型.有了她我们甚至能够来预測一个站点 ...
- EF之POCO应用系列3——延迟加载
EF之POCO应用系列4——延迟加载 当我们进行查询的时候,哪些关系的数据将会被加载到内存呢?所有相关的对象都需要吗?在一些场合可能有意义,例如,当查询的实体仅仅拥有一个相关的子实体,但是,多数情况下 ...
- Mac标识物理位置算法 import Levenshtein mac列表特征值
mac 字符串 与 基准字符串的 Levenshtein 距离,考虑 mac信号强度的时序性,60秒内若干次变化 不引入强度 mac字符串的唯一性 如何排序 基准字符串的选取 同一尺度 都按强度 ...
- Python菜鸟之路:Python基础-Socket基础-1
预热知识 OSI 七层模型 谈到TCP/IP,就不得不说OSI七层模型,OSI 是国际标准化组织(ISO)和国际电报电话咨询委员会(CCITT)联合制定的开放系统互连参考模型,为开放式互连信息系统提供 ...
- Linux安装Nginx使用反向代理
nginx的反向代理功能(自带了反向代理的功能,天生的二道贩子)1.实验环境准备准备2个服务器,都安装好nginx软件nginx1 192.168.13.79 作为web服务器 (理解为火车票售票点) ...