Advanced Fruits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2231    Accepted Submission(s): 1139
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 peach
ananas banana
pear peach
 
Sample Output
appleach
bananas
pearch
 
Source
 
 
/*
ID: LinKArftc
PROG: 1503.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ; int dp[maxn][maxn];
char str1[maxn], str2[maxn];
bool vis1[maxn], vis2[maxn]; int main() {
//input;
int len1, len2;
while (~scanf("%s %s", str1, str2)) {
len1 = strlen(str1);
len2 = strlen(str2);
for (int i = ; i <= len1; i ++) {
for (int j = ; j <= len2; j ++) {
if (str1[i-] == str2[j-]) {
dp[i][j] = dp[i-][j-] + ;
}
else dp[i][j] = max(dp[i-][j], dp[i][j-]);
}
}
memset(vis1, , sizeof(vis1));
memset(vis2, , sizeof(vis2));
if (dp[len1][len2] == ) printf("%s%s\n", str1, str2);
else {
int i = len1, j = len2;
int cur = ;
while (i && j) {
if ((dp[i][j] == dp[i-][j-] + ) && (str1[i-] == str2[j-])) {
i --; j --;
vis1[i] = true;
vis2[j] = true;
} else if (dp[i-][j] > dp[i][j-]) i --;
else j --;
}
i = ; j = ;
while () {
while (!vis1[i] && i < len1) printf("%c", str1[i ++]);
while (!vis2[j] && j < len2) printf("%c", str2[j ++]);
if (vis1[i] && i < len1) {
printf("%c", str1[i ++]);
j ++;
}
if (i >= len1 && j >= len2) break;
}
printf("\n");
}
} return ;
}

HDU1503(LCS,记录路径)的更多相关文章

  1. HDU 1503 Advanced Fruits(LCS+记录路径)

    http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是 ...

  2. LCS记录路径

    还想用hash记录……果然是天真.lcs转移比较简单,每次增加1.每次找是当前-1的就行了. #include <algorithm> #include <iostream> ...

  3. F - LCS 题解(最长公共子序列记录路径)

    题目链接 题目大意 给你两个字符串,任意写出一个最长公共子序列 字符串长度小于3e3 题目思路 就是一个记录路径有一点要注意 找了好久的bug 不能直接\(dp[i][j]=dp[i-1][j-1]+ ...

  4. Educational DP Contest F - LCS (LCS输出路径)

    题意:有两个字符串,求他们的最长公共子序列并输出. 题解:首先跑个LCS记录一下dp数组,然后根据dp数组来反着还原路径,只有当两个位置的字符相同时才输出. 代码: char s[N],t[N]; i ...

  5. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  6. POJ 3436:ACM Computer Factory(最大流记录路径)

    http://poj.org/problem?id=3436 题意:题意很难懂.给出P N.接下来N行代表N个机器,每一行有2*P+1个数字 第一个数代表容量,第2~P+1个数代表输入,第P+2到2* ...

  7. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

  8. hdu1074 状压DP、栈实现记录路径

    题意:给了几门学科作业.它们的截止提交期限(天数).它们的需要完成的时间(天数),每项作业在截止日期后每拖延一天扣一学分,算最少扣的学分和其完成顺序. 一开始做的时候,只是听说过状态压缩这个神奇的东西 ...

  9. hdu 1074(状态压缩dp+记录路径)

    题意:给了n个家庭作业,然后给了每个家庭作业的完成期限和花费的实践,如果完成时间超过了期限,那么就要扣除分数,然后让你找出一个最优方案使扣除的分数最少,当存在多种方案时,输出字典序最小的那种,因为题意 ...

随机推荐

  1. Loadrunner11.0安装与简单使用

    公司开发了APP或者微信小程序啊什么的,都会先进行性能测试,而性能测试一般肯定会来测试接口的压测,并发.Loadrunner是一个很强大的测试工具,它是一种预测系统行为和性能的负载测试工具.通过以模拟 ...

  2. SPRITEKIT游戏框架之关于PHYSICS物理引擎属性

    Spritekit提供了一个默认的物理模拟系统,用来模拟真实物理世界,可以使得编程者将注意力从力学碰撞和重力模拟的计算中解放出来,通过简单地代码来实现物理碰撞的模拟,而将注意力集中在更需要花费精力的地 ...

  3. [USACO18DEC]Fine Dining

    题面 \(Solution:\) 一开始想的是先跑一遍最短路,然后拆点之后再跑一遍,比较两次dis,然后发现拆点后会有负环(可能是我没想对拆点的方法),于是就放弃了拆点法. 我们考虑强制让每头牛选择走 ...

  4. mysql语法总结

    增: 删: 改: 查: 索引: 建: alter table: sql一些常用的经典语句,最后是select as的用法

  5. JSONP跨域jQuery处理整理(附天气数据实例)

    写在前面 跨域的解决方案有多种,其中最常见的是使用同一服务器下的代理来获取远端数据,再通过ajax进行读取,而在这期间经过了两次请求过程,使得获取数据的效率大大降低,这篇文章蓝飞就为大家介绍一下解决跨 ...

  6. STL应用——hdu1702(队列+堆栈)

    水题 练习一下堆栈和队列的使用 #include <iostream> #include <cstdio> #include <algorithm> #includ ...

  7. Java FTP下载文件以及编码问题小结

    问题 之前在开发过程中,遇到了一点问题,我要访问一个FTP服务器去下载文件详细情况如下: 1. 需要传入一个可能为中文的文件名: 2. 通过文件名去FTP上寻找该文件: 3. FTP服务器的命名编码为 ...

  8. asp.net Forms登录核心方法

    登录核心方法: private void Signin(string curUserId) { System.Web.Security.FormsAuthenticationTicket tk = , ...

  9. application/x-www-form-urlencoded从前端到后台

    html <form id="userForm1" enctype="application/x-www-form-urlencoded" method= ...

  10. 【题解】[国家集训队]Crash的数字表格 / JZPTAB

    求解\(\sum_{i = 1}^{n}\sum_{j = 1}^{m}lcm\left ( i,j \right )\). 有\(lcm\left ( i,j \right )=\frac{ij}{ ...