Advanced Fruits
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2158   Accepted: 1066   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

 
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std; int dp[][];
int flag[][];
char str[],str1[];
char P[];
int k;
void print(int l,int r)
{
if(l == && r == ) return;
if(flag[l][r] == )
{
print(l-,r-);
printf("%c",str[l]);
}
else if(flag[l][r] == )
{
print(l-,r);
printf("%c",str[l]);
}
else
{
print(l,r-);
printf("%c",str1[r]);
}
} void LCS()
{
memset(flag,,sizeof(flag));
int n= strlen(str+);
int m = strlen(str1+);
for(int i = ; i <= n; i++) ///初始化不能少
flag[i][] = ;
for(int i = ; i <= m; i++) ///same
flag[][i] = -;
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
if(str[i]==str1[j])
{
dp[i][j]=dp[i-][j-]+;
flag[i][j]=;
}
else
{
if(dp[i-][j]>dp[i][j-])
{
dp[i][j]=dp[i-][j];
flag[i][j]=;
}
else
{
dp[i][j]=dp[i][j-];
flag[i][j]=-;
}
}
}
}
print(n,m);
printf("\n");
}
int main()
{
while(scanf("%s%s",str+,str1+)!=EOF)
{
LCS();
}
return ;
}

poj 2264(LCS)的更多相关文章

  1. LCS(打印全路径) POJ 2264 Advanced Fruits

    题目传送门 题意:两个字符串结合起来,公共的字符只输出一次 分析:LCS,记录每个字符的路径 代码: /* LCS(记录路径)模板题: 用递归打印路径:) */ #include <cstdio ...

  2. poj 1934(LCS)

    转自:http://www.cppblog.com/varg-vikernes/archive/2010/09/27/127866.html 1)首先按照常规的方法求出最长公共子序列的长度也就是用O( ...

  3. POJ 2250(LCS最长公共子序列)

    compromise Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  4. POJ 2217 LCS(后缀数组)

    Secretary Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1655   Accepted: 671 Descript ...

  5. poj 2264 Advanced Fruits(DP)

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

  6. POJ 1159 回文串-LCS

    题目链接:http://poj.org/problem?id=1159 题意:给定一个长度为N的字符串.问你最少要添加多少个字符才能使它变成回文串. 思路:最少要添加的字符个数=原串长度-原串最长回文 ...

  7. LCS POJ 1458 Common Subsequence

    题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...

  8. hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)

    题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...

  9. POJ 2250 Compromise(LCS)

    POJ 2250 Compromise(LCS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#proble ...

随机推荐

  1. 【Python】python中的装饰器——@

    对装饰器本来就一知半解的,今天终于弄清楚了,Python中的装饰器是对装饰者模式的很好运用,简化到骨子里了. python中为什么需要装饰器,看这里:http://www.cnblogs.com/hu ...

  2. 【iOS开发】多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用

    http://blog.csdn.net/crycheng/article/details/21799611 本篇文章主要介绍下多线程下NSOperation.NSBlockOperation.NSI ...

  3. VS2010历史记录清理

    把如下粘贴到文本文件里,另存为批处理文件.(后缀为 *.bat)双击执行就可 @echo off cd \ @echo on @REG Delete HKEY_CURRENT_USER\Softwar ...

  4. lintcode-45-最大子数组差

    45-最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 注意事项 子数组最少包含一个数 样例 给出 ...

  5. C#与Javascript变量、函数之间的相互调用

    原文地址:http://blog.csdn.net/wonsoft/article/details/2595743 C#与Javascript变量.函数之间的相互调用  一.javascript调用C ...

  6. 在 Linux 安装 JDK 和 tomcat(菜鸡级别)

    安装JDK 卸载 OPENJDK rpm -qa|grep jdk  // 查看当前的jdk情况 yum -y remove java java-1.7.0-openjdk* // 卸载openjdk ...

  7. 【题解】CQOI2017老C的键盘

    建议大家还是不要阅读此文了,因为我觉得这题我的解法实在是又不高效又不优美……只是想要记录一下,毕竟是除了中国象棋之外自己做出的组合dp第一题~ 首先如果做题做得多,比较熟练的话,应该能一眼看出这题所给 ...

  8. [bzoj4889] [Tjoi2017]不勤劳的图书管理员

    Description 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被 ...

  9. Cube 找规律

    这道题我们经过简单的推测便可得知3个之前特判,四个之后就成为了一般状况,就是我们每侧都是走整个整个的|_|之后零的走|||. 考试的时候包括平时做题,许多正确的感性比理性证明要强得多. #includ ...

  10. ZOJ3261:Connections in Galaxy War(逆向并查集)

    Connections in Galaxy War Time Limit: 3 Seconds      Memory Limit: 32768 KB 题目链接:http://acm.zju.edu. ...