poj 2264(LCS)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 2158 | Accepted: 1066 | Special Judge |
Description
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
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
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)的更多相关文章
- LCS(打印全路径) POJ 2264 Advanced Fruits
题目传送门 题意:两个字符串结合起来,公共的字符只输出一次 分析:LCS,记录每个字符的路径 代码: /* LCS(记录路径)模板题: 用递归打印路径:) */ #include <cstdio ...
- poj 1934(LCS)
转自:http://www.cppblog.com/varg-vikernes/archive/2010/09/27/127866.html 1)首先按照常规的方法求出最长公共子序列的长度也就是用O( ...
- POJ 2250(LCS最长公共子序列)
compromise Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- POJ 2217 LCS(后缀数组)
Secretary Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1655 Accepted: 671 Descript ...
- poj 2264 Advanced Fruits(DP)
Advanced Fruits Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1944 Accepted: 967 ...
- POJ 1159 回文串-LCS
题目链接:http://poj.org/problem?id=1159 题意:给定一个长度为N的字符串.问你最少要添加多少个字符才能使它变成回文串. 思路:最少要添加的字符个数=原串长度-原串最长回文 ...
- LCS POJ 1458 Common Subsequence
题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...
- hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)
题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...
- POJ 2250 Compromise(LCS)
POJ 2250 Compromise(LCS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#proble ...
随机推荐
- Qscintilla2编译使用
Qscintilla2的下载地址: https://github.com/josephwilk/qscintilla https://riverbankcomputing.com/software/q ...
- Linux C++线程池实例
想做一个多线程服务器测试程序,因此参考了github的一些实例,然后自己动手写了类似的代码来加深理解. 目前了解的线程池实现有2种思路: 第一种: 主进程创建一定数量的线程,并将其全部挂起,此时线程状 ...
- 一种保持顺序的Properties
其实properties有没有顺序都一样 程序都能正常运行 但看着就比较闹心 所以网上找了找 还真有人给了个例子实现读Property的有序 但是删除某些属性之后 写入又有问题 会异常 后来重写了一下 ...
- JDBC数据源的驱动问题
classes12.jar,ojdbc14.jar,ojdbc5.jar和ojdbc6.jar的区别,之间的差异 在使用Oracle JDBC驱动时,有些问题你是不是通过替换不同版本的Oracle ...
- AGC016C +/- Rectangle(构造)
题目大意:给定H,W,h,w四个数,求是否满足矩阵的全部数之和和正数,h行w列之和为负数 如果h和w恰好是H,W的约数,则肯定不存在 否则肯定存在 只需要把h,w内每个元素填的足够大,然后小矩形的最后 ...
- BZOJ5343 [Ctsc2018]混合果汁 【二分 + 主席树】
题目链接 BZOJ5343 题解 明显要二分一下美味度,然后用尽量少的价格去购买饮料,看看能否买到\(L\)升,然后看看能否控制价格在\(g\)内 尽量少的价格,就优先先选完便宜的饮料,由于询问的是一 ...
- BZOJ3533 [Sdoi2014]向量集 【线段树 + 凸包 + 三分】
题目链接 BZOJ3533 题解 我们设询问的向量为\((x_0,y_0)\),参与乘积的向量为\((x,y)\) 则有 \[ \begin{aligned} ans &= x_0x + y_ ...
- Ubuntu下使用mysqli-connect连接mysql时报错:ERROR 1698 (28000): Access denied for user 'root'@'localhost'
LNMP安装好后,写了个index.php文件,里面的内容很简单,就是想测试php与mysql的通信是否正常,代码如下: <?php $host = 'localhost'; $user = ' ...
- NOIP2016愤怒的小鸟 [状压dp]
愤怒的小鸟 题目描述 Kiana 最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于 (0,0) 处,每次 Kiana 可以用它向第一象限发射一只红色的小鸟, ...
- POJ2502:Subway(最短路)
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14634 Accepted: 4718 题目链接:http ...