HDOJ--1503 Advanced Fruits

来源:互联网 发布:方正小标宋简体 mac 编辑:程序博客网 时间:2024/05/21 12:40

Advanced Fruits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2025    Accepted Submission(s): 1035
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 peachananas bananapear peach
 

Sample Output
appleachbananas

pearch

题目大意:额,这道题的大概意思是两个单词拼接成同一个单词,拼接的方式就是把共有的最长子序列抵消掉,然后输出就行了。

详见代码。

ac代码:

#include<stdio.h>#include<string.h>#define max(a,b) a>b?a:bint dp[1010][1010];struct node{int x,y;char c;}rp[1010];int main(){char a[1010],b[1010];while(scanf("%s %s",&a,&b)!=EOF){ //这个循环也是基本的模板,就是DP求最长子序列。 int i,j,k;int len1=strlen(a);int len2=strlen(b);memset(dp,0,sizeof(dp));for(i=1,k=-1;i<=len1;i++)for(j=1;j<=len2;j++){if(a[i-1]==b[j-1]){dp[i][j]=dp[i-1][j-1]+1;}elsedp[i][j]=max(dp[i-1][j],dp[i][j-1]);}if(dp[len1][len2]==0)printf("%s%s",a,b);else{i=len1,j=len2,k=-1;while(i!=0&&j!=0){//这个while循环用来记录子序列在两个原始序列中的位置。 if((dp[i][j]==dp[i-1][j-1]+1)&&a[i-1]==b[j-1]){rp[++k].x=i-1;rp[k].y=j-1;rp[k].c=a[i-1];i--;j--;}else if(dp[i-1][j]>dp[i][j-1])i--;elsej--;}int key=k;i=j=0;for(k=key;k>=0;k--){//这一部分是输出,分三部分输出,第一部分 不是子序列但是在最后一个子序列前面的字符。 while(i!=rp[k].x){printf("%c",a[i]);//第二部分,就是子序列这一部分的字符。 i++;}while(j!=rp[k].y){printf("%c",b[j]);j++;}printf("%c",rp[k].c);i++;j++;}printf("%s%s\n",a+rp[0].x+1,b+rp[0].y+1);//第三部分就是子序列之后的字符。 }}return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 在4楼上课地震了怎么办 微鲸电视待机打不开怎么办 苹果手机微信听不到语音怎么办 电信怎么办腾讯频看电视不要流量 孩子做事效率低怎么办教案 孩子做事效率低怎么办读后感 做事效率低总是焦虑怎么办 红米手机黑屏打不开怎么办 手机萤石云视频下载打不开怎么办 oppo打开网页视频慢怎么办? 晒课显示待提交怎么办 华为手机无法访问移动网络怎么办 晒课上传课堂实录太大怎么办 手机酷狗音乐下载要钱怎么办 手机酷狗下载要钱怎么办 酷我音乐没有声音怎么办 手机酷我音乐没有声音怎么办 酷我音乐歌曲下载收费怎么办 网易云下载超过每日上限怎么办 全民k歌领不了花怎么办 安卓全民k歌延迟怎么办 全民k歌唱歌延迟怎么办 全民k歌耳机延迟怎么办 word文档打开是乱码怎么办 全民k歌不能录音怎么办 全民k歌登录不上怎么办 平果手机迅雷闪退怎么办 电脑打开央视影音死机怎么办 先锋影音二级网页打不开怎么办 手机qq音乐登录失效怎么办 酷狗账号忘记了怎么办 手机qq音乐听不了歌怎么办 第一试用网密码忘了怎么办 玩h1z1画面卡顿怎么办 uu跑腿抢不到单怎么办 比特币加密忘了怎么办 路虎发现cd卡死怎么办 苹果手机帐号被锁定怎么办 苹果手机帐号锁定了怎么办 微博帐号被锁定怎么办 微博显示帐号被锁定怎么办