dp-LCS(递归输出最短合串)
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.
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.
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.
/*
* Author: ry
* Created Time: 2017/9/4 21:04:24
* File Name: 1.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int mm = 1e6+5;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long ll t_cnt;
void t_st(){t_cnt=clock();}
void t_ot(){printf("you spent : %lldms\n", clock()-t_cnt);}
//开始t_st();
//结束t_ot(); char a[105], b[105];
int dp[105][105];
int mark[105][105]; void fun(int i , int j){
if (!i && !j) return; if (mark[i][j] == 0){
fun(i-1,j-1);
printf("%c", a[i]);
}
else if (mark[i][j] == 1){
fun(i-1, j);
printf("%c", a[i]);
}
else {
fun(i, j-1);
printf("%c", b[j]);
} } int main() { while (~scanf("%s%s", a, b)){
int len1 = strlen(a);
int len2 = strlen(b);
for(int i = len1; i > 0; i--)
a[i] = a[i-1];
for(int i = len2; i > 0; i--)
b[i] = b[i-1]; memset(dp, 0, sizeof(dp));
for(int i = 1; i <= len1; i++)
mark[i][0] = 1;
for(int j = 1; j <= len2; j++)
mark[0][j] = -1; for (int i = 1; i <= len1; i++){
for(int j = 1; j <= len2; j++) {
if (a[i] == b[j]) {
dp[i][j] = dp[i-1][j-1] + 1;
mark[i][j] = 0;
}
else if (dp[i-1][j] >= dp[i][j-1]){
dp[i][j] = dp[i-1][j];
mark[i][j] = 1;
}
else {
dp[i][j] = dp[i][j-1];
mark[i][j] = -1;
}
}
} int i = len1, j = len2;
fun(i, j);
printf("\n")
}
return 0;
}
dp-LCS(递归输出最短合串)的更多相关文章
- 【状态压缩dp】1195: [HNOI2006]最短母串
一个清晰的思路就是状压dp:不过也有AC自动机+BFS的做法 Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T ...
- [bzoj1195][HNOI2006]最短母串_动态规划_状压dp
最短母串 bzoj-1195 HNOI-2006 题目大意:给一个包含n个字符串的字符集,求一个字典序最小的字符串使得字符集中所有的串都是该串的子串. 注释:$1\le n\le 12$,$1\le ...
- bzoj 1195: [HNOI2006]最短母串 爆搜
1195: [HNOI2006]最短母串 Time Limit: 10 Sec Memory Limit: 32 MBSubmit: 894 Solved: 288[Submit][Status] ...
- P2322 [HNOI2006]最短母串问题
P2322 [HNOI2006]最短母串问题 AC自动机+bfs 题目要求:在AC自动机建的Trie图上找到一条最短链,包含所有带结尾标记的点 因为n<12,所以我们可以用二进制保存状态:某个带 ...
- [HNOI2006]最短母串问题 --- AC自动机 + 隐式图搜索
[HNOI2006]最短母串问题 题目描述: 给定n个字符串(S1,S2.....,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,......,Sn)都是T的子串. 输入格式: 第 ...
- uva 10453 dp/LCS变形
https://vjudge.net/problem/UVA-10453 给出一个字符串,问最少添加几个字符使其变为回文串,并输出任意一种答案.就是一个类似于LCS的题目,而且简化了一下,只会出现三种 ...
- 2782: [HNOI2006]最短母串
2782: [HNOI2006]最短母串 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3 Solved: 2[Submit][Status][Web ...
- BZOJ1195[HNOI2006]最短母串——AC自动机+BFS+状态压缩
题目描述 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 输入 第一行是一个正整数n(n<=12),表示给定的字符串的 ...
- BZOJ 1195: [HNOI2006]最短母串
1195: [HNOI2006]最短母串 Time Limit: 10 Sec Memory Limit: 32 MBSubmit: 1346 Solved: 450[Submit][Status ...
随机推荐
- 4-2 setting中一定要将ROBOTSTXT_OBEY = False的注释去掉
# Obey robots.txt rules##默认遵循robots协议的,默认去读取每个网站上的robots协议ROBOTSTXT_OBEY = False
- java 泛型的嵌套(map例子)
package july7; //泛型加Map的输出! import java.util.Iterator; import java.util.Map; import java.util.Map.En ...
- mysql ”Invalid use of null value“ 解决方法
1.问题描述 因为要更改"information"表中的"编号"列为非空,使用数据库查询语句“alter table information modify '编 ...
- Linux 内核 usb_control_msg 接口
usb_control_msg 函数就像 usb_bulk_msg 函数, 除了它允许一个驱动发送和结束 USB 控制信息: int usb_control_msg(struct usb_device ...
- dotnet 获取指定进程的输入命令行
本文告诉大家如何在 dotnet 获取指定的进程的命令行参数 很多的程序在启动的时候都需要传入参数,那么如何拿到这些程序传入的参数? 我找到两个方法,一个需要引用 C++ 库支持 x86 和 x64 ...
- c#中索引器
https://zhidao.baidu.com/question/59675980.html 不是必要的..相当于数学中的一个函数
- 激励函数 (Activation)
softplus是有关概率的巴拉巴拉? Torch 中的激励函数有很多, 不过我们平时要用到的就这几个. relu, sigmoid, tanh, softplus. 那我们就看看他们各自长什么样啦. ...
- com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1680 > 1024). You can change this value on the server by setting the max_allowed_packet' variable.
这个错误是由于mysql的一个系统参数max_allowed_packet设置的值过小引起的 解决这个错误的方法就是修改这个参数的值, linux系统中我们在etc目录下找到my.cnf这个文件,打开 ...
- eclipse中SSM(maven)项目搭建全过程+实现用户登录功能
项目创建之前确保eclipse中已经配置好了jdk,tomcat,maven如果没有配置下面有配置教程的链接 eclipse中配置jdk的教程url:http://www.cnblogs.com/ ...
- 洛谷$P$1486 郁闷的出纳员 $[NOI2004]$ $splay$
正解:$splay$ 解题报告: 传送门! 依然先考虑要呲呲些什么操作鸭$QwQ$ 其实就只要一个删除区间,一个查询第$k$大,还一个插入就欧克? 删除区间的话直接旋转下根什么的然后直接把子树删了就好 ...