Advanced Fruits
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1944   Accepted: 967   Special Judge

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

Source

题意:

给你两个长度不超过100的字符串A,B。

要你找出一个最短的字符串C。

使得A,B都是C的子序列(不一定是子串).

思路:

開始看题目里面写的是sub-strings然后看例子百思不得其解。没办法。大胆如果出题人这两个概念分不清。思索片刻后想出了O(n^3)的算法。vis[k][i][j]表示C串长为k时包括了A串的前i-1个字符和B串的前j-1个位置。

那么

if(A[i]==B[j])

vis[k+1][i+1][j+1]=1;

else

vis[k+1][i+1][j]=1,vis[k+1][i][j+1]=1;

然后找到最小的k即可了。

比赛的时候各种犯傻。调试了非常久才调出来。

下来又想了下这道题。

认为先前的做法简直可爱到极致。为何生拉硬扯加个k.直接dp[i][j]表示包括了A串的前i-1个字符和B串的前j-1个位置的C串的最短长度。

if(A[i]==B[j])

dp[i][j]=dp[i-1][j-1]+1;

else

dp[i][j]=min(dp[i][j-1],dp[i-1][j])+1;

然后记录转移的方向就好了。

具体见代码:

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
int dp[150][150],path[150][150];
char sa[150],sb[150];
void print(int i,int j)
{
if(i==0&&j==0)
return;
if(path[i][j]==1)
{
print(i-1,j);
printf("%c",sa[i]);
}
else if(path[i][j]==-1)
{
print(i,j-1);
printf("%c",sb[j]);
}
else
{
print(i-1,j-1);
printf("%c",sa[i]);
}
}
int main()
{
int i,j,la,lb; while(~scanf("%s%s",sa+1,sb+1))
{
la=strlen(sa+1);
lb=strlen(sb+1);
for(i=1;i<=la;i++)
dp[i][0]=i,path[i][0]=1;
for(i=1;i<=lb;i++)
dp[0][i]=i,path[0][i]=-1;
for(i=1;i<=la;i++)
for(j=1;j<=lb;j++)
{
dp[i][j]=INF;
if(sa[i]==sb[j])//相等仅仅用添加一个字符
dp[i][j]=dp[i-1][j-1]+1,path[i][j]=0;
else
{
if(dp[i][j-1]<dp[i-1][j])//添加sb[j]
dp[i][j]=dp[i][j-1]+1,path[i][j]=-1;
else//添加sa[i]
dp[i][j]=dp[i-1][j]+1,path[i][j]=1;
}
}
print(la,lb);
printf("\n");
}
return 0;
}

比赛的做法:

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int vis[210][150][150],path[210][150][150];
char sa[150],sb[150];
void print(int st,int i,int j)
{
//printf("st %d i %d j %d\n",st,i,j);
if(st==1)
{
if(path[st][i][j]==1)
printf("%c",sa[i-1]);
else if(path[st][i][j]==-1)
printf("%c",sb[j-1]);
else
printf("%c",sa[i-1]);
return;
}
if(path[st][i][j]==1)
{
print(st-1,i-1,j);
printf("%c",sa[i-1]);
}
else if(path[st][i][j]==-1)
{
print(st-1,i,j-1);
printf("%c",sb[j-1]);
}
else
{
print(st-1,i-1,j-1);
printf("%c",sa[i-1]);
}
}
int main()
{
int la,lb,i,j,k,len;
while(~scanf("%s%s",sa,sb))
{
la=strlen(sa);
lb=strlen(sb);
memset(vis,0,sizeof vis);
len=la+lb;
vis[1][1][0]=vis[1][0][1]=1,path[1][1][0]=1,path[1][0][1]=-1;
if (sa[0]==sb[0]) vis[1][1][1]=1,path[1][1][1]=0;
for(k=1;k<=len;k++)
{
if(vis[k][la][lb])
break;
//printf("k %d\n",k);
for(i=0;i<=la;i++)
for(j=0;j<=lb;j++)
{
if(!vis[k][i][j])
continue;
//printf("%d %d\n",i,j);
if(i<la&&j<lb&&sa[i]==sb[j]&&!vis[k+1][i+1][j+1])
vis[k+1][i+1][j+1]=1,path[k+1][i+1][j+1]=0;
else
{
if(!vis[k+1][i+1][j])
vis[k+1][i+1][j]=1,path[k+1][i+1][j]=1;
if(!vis[k+1][i][j+1])
vis[k+1][i][j+1]=1,path[k+1][i][j+1]=-1; }
}
}
//printf("k %d\n",k);
print(k,la,lb);
printf("\n");
}
return 0;
}

最新文章

  1. 【MVVM DEV】DataColumn中的TextBox与ComboBox的并存
  2. Java 中 手动抛出异常: throw new Exception(&quot;错误信息&quot;) 错误信息的获得
  3. [字符哈希] POJ 3094 Quicksum
  4. 【算法与数据结构】冒泡、插入、归并、堆排序、快速排序的Java实现代码
  5. centos7 添加图形界面的功能
  6. Java集合之Collection接口
  7. HDU1518 Square(DFS)
  8. vmware虚拟机挂起后无法再恢复(转)
  9. mvvm结构中数据的关联----wpf
  10. rdlc报表
  11. SELECT ... LOCK IN SHARE MODE和SELECT ... FOR UPDATE locks在RR模式下可以看到最新的记录
  12. 在WINDOWS下 三步快速配置 eclipse c++ 环境
  13. CF #405 (Div. 2) B. Bear ad Friendship Condition (dfs+完全图)
  14. js-获取两个字符串日期的相隔周
  15. Spark第一个应用程序
  16. [Swift]LeetCode56. 合并区间 | Merge Intervals
  17. 汉明码(Hamming)编码与纠错原理
  18. Logging常用handlers的使用
  19. 在整合spring和mongodb中,提示at org.springframework.data.mapping.model.BasicPersistentEntity.findAnnotation(
  20. Maven入门指南④:仓库

热门文章

  1. win10 设置软件开机启动项失效
  2. python os模块部分摘录
  3. 紫书第五章训练3 D - Throwing cards away I
  4. hdu2063 二分图匹配,匈牙利算法
  5. PTA 09-排序3 Insertion or Heap Sort (25分)
  6. (绝对有用)iOS获取UUID,并使用keychain存储
  7. BZOJ 4823 [Cqoi2017]老C的方块 ——网络流
  8. BZOJ 4001 [TJOI2015]概率论 ——找规律
  9. 标准C程序设计七---26
  10. 使用 ftrace 调试 Linux 内核,第 2 部分