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. 图书 Framework 设计指南: 可重用 .NET 库的约定、惯用法和模式 引出资料

    文章:框架设计准则     --微软 地址:https://docs.microsoft.com/zh-cn/dotnet/standard/design-guidelines/index

  2. 文件系统之 stat与access

    stat命令 stat既有命令也有同名函数,用来获取文件Inode里主要信息,所以stat命令的输出信息比ls命令的输出信息要更详细,stat 跟踪符号链接,lstat不跟踪符号链接,其中会输出对应文 ...

  3. PAT 1040 有几个PAT

    https://pintia.cn/problem-sets/994805260223102976/problems/994805282389999616 字符串 APPAPT 中包含了两个单词 PA ...

  4. 【Linux】——实用命令

    [前言] Linux的命令可以分为文件存取.目录操作.进程管理.权限管理.磁盘操作等内容,大量的命令方便了用户进行更快捷更高效的工作.但有一点需要说明的是,如果不采用linux的命令,也可以完成相应的 ...

  5. springboot ueditor 使用心得

    1.将ueditor引入项目中会发现,图片不能上传,返回值意思是因配置文件错误,导致图片无法上传 默认情况是使用jsp初始配置文件,这就需要项目支持jsp解析 在maven中引入 <!--添加对 ...

  6. Xinetd服务的安装与配置

    Xinetd服务的安装与配置 http://blog.chinaunix.net/uid-21411227-id-1826885.html 1.什么是xinetd xinetd即extended in ...

  7. [NOIP2017 TG D2T2]宝藏(模拟退火)

    题目大意:$NOIPD2T2$宝藏 题解:正常做法:状压DP .这次模拟退火,随机一个排列,$O(n^2)$贪心按排列的顺序加入生成树 卡点:没开$long\;long$,接受较劣解时判断打错,没判$ ...

  8. Spring AOP前置通知实例讲解与AOP详细解析

    一.引出问题 有个接口TestServiceInter,有两个实现方法TestService和Test2Service.他们都有sayHello():我们的需求是在调用这两个方法之前,要先完成写日志的 ...

  9. [Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  10. 深入浅出JavaScript变量作用域

    在学习JavaScript的变量作用域之前,我们应当明确几点: JavaScript的变量作用域是基于其特有的作用域链的. JavaScript没有块级作用域. 函数中声明的变量在整个函数中都有定义. ...