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. Sersync实时备份服务部署实践

  2. 1107 Social Clusters (30 分)(并查集)

    并查集的基本应用 #include<bits/stdc++.h> using namespace std; ; vector<int>vec[N]; int p[N]; con ...

  3. CVPR2018: Generative Image Inpainting with Contextual Attention 论文翻译、解读

    注:博主是大四学生,翻译水平可能比不上研究人员的水平,博主会尽自己的力量为大家翻译这篇论文.翻译结果仅供参考,提供思路,翻译不足的地方博主会标注出来,请大家参照原文,请大家多多关照. 转载请务必注明出 ...

  4. BZOJ 2669 CQOI2012 局部极小值 状压dp+容斥原理

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2669 题意概述:实际上原题意很简洁了我就不写了吧.... 二话不说先观察一下性质,首先棋盘 ...

  5. Java语言课程设计

    一.项目简介 本实验是对图形用户界面,布局管理器的综合应用,理解Java的处理机制,编写独立运行的窗口 二.项目采用技术 GUI,JAVA 三.功能需求分析 1.使用用户图形界面 2.能够实现年月份的 ...

  6. 并发(三) CountDownLatch

    CountDownLatch 和CyclicBarrier的区别是,CyclicBarrier可以循环使用,CountDownLatch不可以:CyclicBarrier可以有一个Runnable参数 ...

  7. HDU 6191 Query on A Tree(可持久化Trie+DFS序)

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  8. [洛谷P1747]好奇怪的游戏

    题目大意:有两匹马,马可以走"日",也可以像象走"田",求它走到(1,1)的步数. 题解:bfs 卡点:边界判断成了可以走到(0,y)或(x,0) C++ Co ...

  9. Seajs的用法

    以前经常听到Seajs,但是没深入了解过,不清楚到底是用做哪个方面,后来调组到M站做开发,发现项目用到了Seajs,便去了解下 SeaJS是一个遵循CMD规范的JavaScript模块加载框架,可以实 ...

  10. js金额转大写数字

    //金额转大写数字 const intToChinese = money => { //汉字的数字 let cnNums = new Array('零', '壹', '贰', '叁', '肆', ...