最长公共子序列(LCS) Medium2
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.
InputEach 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.
OutputFor 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
#include<iostream>
#include<cstring>
#include<cstdio> using namespace std;
const int N = + ;
char str1[N], str2[N];
int dp[N][N], Mark[N][N]; void Work(){
memset(dp, , sizeof(dp)); int len_1 = strlen(str1), len_2 = strlen(str2);
for(int i = ;i<=len_1;i++) Mark[i][] = ;
for(int i = ;i<=len_2;i++) Mark[][i] = -;
for(int i = ; i <= len_1; i++)
for(int j = ; j <= len_2; j++)
if(str1[i-] == str2[j-]){ dp[i][j] = dp[i-][j-] + ; Mark[i][j] = ;}
else if(dp[i-][j] >= dp[i][j-]) {dp[i][j] = dp[i-][j]; Mark[i][j] = ;}
else dp[i][j] = dp[i][j-],Mark[i][j] = -;
}
void Solve(int i, int j){
if(i == && j == ) return;
if(Mark[i][j] > ){
Solve(i - , j - );
printf("%c", str1[i-]);
}else if(Mark[i][j] == ){
Solve(i-, j);
printf("%c", str1[i-]);
}else{
Solve(i, j-);
printf("%c", str2[j-]);
}
}
int main(){
while(scanf("%s %s", str1, str2) == ){
Work();
Solve(strlen(str1), strlen(str2));
printf("\n");
}
}
最长公共子序列(LCS) Medium2的更多相关文章
- 1006 最长公共子序列Lcs
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdks ...
- 动态规划之最长公共子序列LCS(Longest Common Subsequence)
一.问题描述 由于最长公共子序列LCS是一个比较经典的问题,主要是采用动态规划(DP)算法去实现,理论方面的讲述也非常详尽,本文重点是程序的实现部分,所以理论方面的解释主要看这篇博客:http://b ...
- 编程算法 - 最长公共子序列(LCS) 代码(C)
最长公共子序列(LCS) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定两个字符串s,t, 求出这两个字符串最长的公共子序列的长度. 字符 ...
- C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解
版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- 51Nod 1006:最长公共子序列Lcs(打印LCS)
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
- 51nod 1006 最长公共子序列Lcs 【LCS/打印path】
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
- 每日一题-——最长公共子序列(LCS)与最长公共子串
最长公共子序列(LCS) 思路: 代码: def LCS(string1,string2): len1 = len(string1) len2 = len(string2) res = [[0 for ...
- 51nod 1006:最长公共子序列Lcs
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
随机推荐
- Python简单雷达图绘制
import numpy as npimport matplotlib.pyplot as pltimport matplotlibmatplotlib.rcParams['font.family'] ...
- 私有ip地址知多少?
1.私有ip的由来 在现在的网络中,ip地址分为公网ip地址和私有ip地址.公网ip是在Internet中使用的ip地址,而私有ip地址是在局域网中使用,在Internet上不使用. 由于我们目前使用 ...
- Sublime Text3的Package Control安装教程,及报错解决There Are No Packages Available For Installation
一.Package Control的安装 Sublime 有很多插件,这些插件为我们写python代码提供了非常强大的功能,这些插件需要单独安装.而安装这些插件最方便的方法就是通过Package Co ...
- json 的简单应用
今天做爬虫时的一点盲区 :字符串, 字典,写到同一个文件中.难以利用!比如这样的数据:str = “hi,budy. i like 52pojie!”dirt = {“陈墨”:["男&quo ...
- msf生成后门拿shell
0X01简介 这里我本来想学习 msf利用漏洞反弹shell的 但是 没有靶机....等找了靶机在弄吧 kali 172.18.5.118 靶机 172.18.5.240 先尝试能不能ping通 好 ...
- Mac OS下Flutter环境搭建记录,VS Code开发
安装Flutter 获取FlutterSDK 终端cd进入SDK安装目录,比如 cd ~/FlutterSDK 由于在国内访问Flutter有时可能会受到限制,Flutter官方为中国开发者搭建了临时 ...
- Linux_系统时间管理
目录 目录 时间管理 date指令 系统时间设置timedatectl指令 本地时间同步 时间服务器NTP RHEL6 RHEL7 计划任务 一次性计划任务 at指令 限制用户建立一次性计划任务 周期 ...
- CSS样式div
页面中,有很多样式标签:div标签,对标签定位的地方有: 1.<head>标签里加<style>标签,在<style>标签中添加样式.如: <style> ...
- Python学习之==>接口开发
一.开发接口的作用 1.在别的接口没有开发完成的时候可以模拟一些接口以便测试已经开发完成的接口,例如假的支付接口,模拟支付成功.支付失败. 2.了解接口是如何实现的:数据交互.数据返回 3.开发给别人 ...
- 【SpringMVC】---RequestMapping、Ant 路径、PathVariable 注解、HiddenHttpMethodFilter 过滤器、用 POJO 作为参数
一.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...