Advanced Fruits(HDU 1503 LCS变形)
Advanced Fruits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2358 Accepted Submission(s): 1201
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.
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define Max 102
int dp[Max][Max];
int mark[Max][Max];
char s[Max],t[Max];
int len1,len2;
void LCS() //计算LCS,并用mark标记数组记录dp数组的传递过程
{
int i,j;
memset(mark,,sizeof(mark));
memset(dp,,sizeof(dp));
for(i=;i<=len1;i++)
{
for(j=;j<=len2;j++)
{
if(s[i-]==t[j-])
{
dp[i][j]=dp[i-][j-]+;
// cout<<s[i-1]<<" ";
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]=;
}
}
} return;
}
void output(int i,int j) //回溯输出
{
/*if(i==0&&j!=0)
{
output(i,j-1);
//printf("%c",t[j-1]);
}
else if(i!=0&&j==0)
{
output(i-1,j);
//printf("%c",s[i-1]);
}
else if(i==0&&j==0)
return;*/
if(i==||j==)
return;
if(mark[i][j]==)
{
output(i-,j-);
printf("%c",s[i-]);
}
else if(mark[i][j]==)
{
output(i-,j);
//printf("%c",s[i-1]);
}
else
{
output(i,j-);
//printf("%c",t[j-1]);
}
return;
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%s%s",s,t)!=EOF)
{
len1=strlen(s),len2=strlen(t);
LCS();
output(len1,len2);
printf("\n");
}
}
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define Max 102
int dp[Max][Max];
int mark[Max][Max];
char s[Max],t[Max];
int len1,len2;
void LCS() //计算LCS,并用mark标记数组记录dp数组的传递过程
{
int i,j;
memset(mark,,sizeof(mark));
memset(dp,,sizeof(dp));
for(i=;i<=len1;i++)
{
for(j=;j<=len2;j++)
{
if(s[i-]==t[j-])
{
dp[i][j]=dp[i-][j-]+;
// cout<<s[i-1]<<" ";
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]=;
}
}
} return;
}
void output(int i,int j) //回溯输出
{
if(i==&&j!=)
{
output(i,j-);
printf("%c",t[j-]);
}
else if(i!=&&j==)
{
output(i-,j);
printf("%c",s[i-]);
}
else if(i==&&j==)
return;
else if(mark[i][j]==)
{
output(i-,j-);
printf("%c",s[i-]);
}
else if(mark[i][j]==)
{
output(i-,j);
printf("%c",s[i-]);
}
else
{
output(i,j-);
printf("%c",t[j-]);
}
return;
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%s%s",s,t)!=EOF)
{
len1=strlen(s),len2=strlen(t);
LCS();
output(len1,len2);
printf("\n");
}
}
Advanced Fruits(HDU 1503 LCS变形)的更多相关文章
- hdu 1503 LCS输出路径【dp】
hdu 1503 不知道最后怎么输出,因为公共部分只输出一次.有人说回溯输出,感觉好巧妙!其实就是下图,输出的就是那条灰色的路径,但是初始时边界一定要初始化一下,因为最第一列只能向上走,第一行只能向左 ...
- Advanced Fruits HDU杭电1503【LCS的保存】
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
- hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏
a typical variant of LCS algo. the key point here is, the dp[][] array contains enough message to de ...
- hdu 1080(LCS变形)
Human Gene Functions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- hdu 1243(LCS变形)
反恐训练营 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 1503:Advanced Fruits(动态规划 DP & 最长公共子序列(LCS)问题升级版)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 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
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1503 Advanced Fruits 最长公共子序列 *
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- CoreData学习-最好的一片文章
CoreData学习-最好的一片文章 分类: IOS重新上路2014-05-25 18:00 1937人阅读 评论(0) 收藏 举报 目录(?)[+] 写的很好的一篇教程,我什么时候能写出这么 ...
- App Store审核指南中文版(2014.10.11更新)
App Store审核指南中文版(2014.10.11更新) 2014-10-11 16:36 编辑: suiling 分类:AppStore研究 来源:CocoaChina 2 8657 App ...
- Android使用xml中定义的动画效果
Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.zqrl_out); animation.setFil ...
- ajax get/post
xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader("Content-Type", " ...
- 转:PO BO VO DTO POJO DAO概念及其作用
J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨论问题的时候,三分钟就被人家满口的专业术语喷晕了,PO VO BO DTO POJO DAO,一大堆的就来了(听过老罗对这种现象的批判的朋 ...
- 常用监控SQL
1.---监控等待事件 select SESSION_ID,NAME,P1,P2,P3,WAIT_TIME,CURRENT_OBJ#,CURRENT_FILE#,CURRENT_BLOCK# ...
- GET: https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login? loginicon=true &uuid=odcptUu2JA==&tip=0
GET: https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login? loginicon=true &uuid=odcptUu2JA==&am ...
- 【剑指offer】面试题28:字符串的排列
题目: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述:输入一 ...
- 【POJ3006】Dirichlet's Theorem on Arithmetic Progressions(素数筛法)
简单的暴力筛法就可. #include <iostream> #include <cstring> #include <cmath> #include <cc ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅰ
许多应用程序都需要处理有序的元素,但不一定要求他们全部有序,或者是不一定要以此就将他们排序.很多情况下我们会手机一些元素,处理当前键值最大的元素,然后再收集更多的元素,再处理当前键值最大的元素.如此这 ...