博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Common Subsequence
阅读量:5997 次
发布时间:2019-06-20

本文共 2287 字,大约阅读时间需要 7 分钟。

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, ..., ik > of indices of X such that for all j = 1,2,...,k, x
ij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
Input
The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.
Output
For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Sample Input
abcfbc         abfcabprogramming    contest abcd           mnp
Sample Output
420 求两个字符串的最长公共子序列。
#include
#include
#include
#include
#include
using namespace std;char a[5000];char b[1050];char c[1050];int dp[1050][1050]={
0};int main(){ while(scanf("%s %s",b,c)!=EOF) { memset(dp,0,sizeof(dp)); int i; /* for(i=0;a[i]>='a'&&a[i]<='z';i++) b[i]=a[i]; b[i]='\0'; int l=0; for(;a[i]!='\0';i++) if(a[i]>='a'&&a[i]<='z') c[l++]=a[i]; c[l]='\0';*/ for(i=0;c[i]!='\0';i++) if(c[i]==b[0]) { dp[i][0]=1; break; } for(;c[i]!='\0';i++) dp[i][0]=1; for(i=0;b[i]!='\0';i++) if(c[0]==b[i]) { dp[0][i]=1; break; } for(;b[i]!='\0';i++) dp[0][i]=1; for(int i=1;b[i]!='\0';i++) { for(int j=0;c[j]!='\0';j++) { if(c[j]==b[i]) { dp[j][i]=max(dp[j-1][i-1]+1,dp[j][i-1]); } else dp[j][i]=max(dp[j-1][i],dp[j][i-1]); } } int l1=strlen(b); int l2=strlen(c); printf("%d\n",dp[l2-1][l1-1]); } return 0;}

 

转载于:https://www.cnblogs.com/xzxj/p/7236319.html

你可能感兴趣的文章
1.numpy的用法
查看>>
std::vector push_back报错Access violation
查看>>
在运行时让用户选择连接字符串
查看>>
JUNIT测试通过,EMMA通不过,java.lang.NoClassDefFoundError: oracle/security/pki/OracleWallet
查看>>
hyperref
查看>>
启明星辰加入微软MAPP计划 保护用户更有力
查看>>
字体大小自适应纯css解决方案
查看>>
【十五分钟Talkshow】为什么新浪微博的输入文本框具有记忆功能
查看>>
【项目经验】iphone自定义状态栏
查看>>
捕获异常 .
查看>>
数据结构---->图的应用(拓扑排序,关键路径)
查看>>
设计模式---->依赖倒置原则
查看>>
[珠玑之椟]字符串和序列:左移、哈希、最长重复子序列的后缀数组解法、最大连续子序列...
查看>>
高性能 TCP & UDP 通信框架 HP-Socket v3.2.3
查看>>
使用最新最酷的安卓开发技术
查看>>
搭建emacs的go编程语言环境
查看>>
R语言绘图边框的单位
查看>>
微信小程序入门(六)
查看>>
豆瓣源安装requirements.txt
查看>>
一张表看懂英式音标和美式音标的差异
查看>>