poj 2264 LCS(同时包含两个串的最短父串)
来源:互联网 发布:linux虚拟机 cpu 编辑:程序博客网 时间:2024/06/05 18:39
题意:给定两个字符串a和b,求一个长度最小的同时包含a串和b串的串。
输入:
apple peach
ananas banana
pear peach
输出:
appleach
bananas
pearch
思路:思路比较简单,先求出两个串的LCS,然后将两个串不在LCS中的字符在相应的LCS的“空隙”中输出。
#include <stdio.h>#include <string.h>#define max(a,b) ((a)>(b)?(a):(b))#define N 105int dp[N][N],path[N][N],n,m;char s[N],t[N],p[N];void getpath(int a,int b){if(!a || !b)return ;if(!path[a][b]){p[dp[a][b]] = s[a-1];getpath(a-1,b-1);}else if(path[a][b] == 1)getpath(a-1,b);elsegetpath(a,b-1);}int main(){freopen("a.txt","r",stdin);while(scanf("%s %s",s,t)!=EOF){int i,j,k;n = strlen(s);m = strlen(t);memset(dp,0,sizeof(dp));memset(path,0,sizeof(path));for(i = 1;i<=n;i++)//求LCSfor(j = 1;j<=m;j++){if(s[i-1] == t[j-1])dp[i][j] = dp[i-1][j-1]+1;else{if(dp[i-1][j]>=dp[i][j-1]){dp[i][j] = dp[i-1][j];path[i][j] = 1;}else{dp[i][j] = dp[i][j-1];path[i][j] = 2;}}}getpath(n,m);//得到LCS序列//i,j分别为s和t串的指针,k为LCS的指针for(i=j=0,k = 1;k<=dp[n][m];k++,i++,j++){while(s[i]!=p[k])printf("%c",s[i++]);while(t[j]!=p[k])printf("%c",t[j++]);printf("%c",p[k]);}//记得最后可能残余字符,需要输出while(i<n)printf("%c",s[i++]);while(j<m)printf("%c",t[j++]);putchar('\n');}return 0;}
0 0
- poj 2264 LCS(同时包含两个串的最短父串)
- jQuery 选择同时包含两个class的元素
- jQuery、zepto选择同时包含两个class的元素
- poj 1159 (LCS)
- 求两个数组的最长公共子串长度(LCS)
- LCS求两个字符串的最大公共子串
- 求两个字符串的最长公共子串(LCS)
- 求两个字符串的最长公共子串(LCS)
- 求两个字符串的最长公共子串(LCS)
- 两个字符串最长的公共字串问题(LCS)
- 求两个字符串最长公共子串LCS(其它)
- poj 2264 Advanced Fruits lcs
- poj 2264 Advanced Fruits dp解LCS的对偶问题
- poj 2250 Compromise (LCS)
- POJ 1159 Palindrome (LCS)
- poj 2250 Compromise(LCS)
- POJ 1159 Palindrome (LCS)
- POJ 1458 (简单LCS)
- iOS 自定义TabBarController
- logback 配置日志写入数据库中
- android蓝牙编程问题
- bitnami svn apache与php5 安装
- Linux上iptables防火墙的基本应用教程
- poj 2264 LCS(同时包含两个串的最短父串)
- 全局变量与局部变量定于一个数组赋值后反汇编区别
- malloc_free复杂结构
- 第一篇博客测试
- Linux网卡重命名
- 关于KK版本语言列表[Developer]Accented English问题
- Android手掌抑制功能的实现
- 4G新业务--RCS简介
- SQL各個命令的作用