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

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

Problem Description

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, xij = 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. 
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. 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 abfcab
programming contest
abcd mnp
 

 

Sample Output

4 2 0
 
求最长公共子序列
 
1 #include
2 #include
3 #include
4 using namespace std; 5 int dp[10000][10000]; 6 int main() 7 { 8 int lena,lenb; 9 char a[10000],b[10000];10 while(scanf("%s %s",&a,&b)!=EOF)11 {12 int i,j;13 lena=strlen(a);14 lenb=strlen(b);15 for(i = 1 ; i <= lena ; i++)16 {17 for(j = 1 ; j <= lenb ; j++)18 {19 if(a[i-1] == b[j-1])20 dp[i][j]=dp[i-1][j-1]+1;21 else22 dp[i][j]=max(dp[i][j-1],dp[i-1][j]);23 }24 }25 printf("%d\n",dp[lena][lenb]);26 }27 }

 

转载于:https://www.cnblogs.com/yexiaozi/p/5766021.html

你可能感兴趣的文章
腾讯2017暑期实习编程题3
查看>>
整理收藏一份PHP高级工程师的笔试题
查看>>
Intellij IDEA 构建Spring Web项目 — 用户登录功能
查看>>
[AHOI2013]作业
查看>>
[bzoj 4241]历史研究
查看>>
git push被忽略的文件 处理
查看>>
C#中用ILMerge将所有引用的DLL打成一个DLL文件
查看>>
PHP生成HTML静态页面
查看>>
服务器启动django
查看>>
Makefile 中:= ?= += =的区别【转】
查看>>
使用makecontext实现用户线程【转】
查看>>
Comet:基于 HTTP 长连接的“服务器推”技术
查看>>
BZOJ 2733: [HNOI2012]永无乡 启发式合并treap
查看>>
四种方法校验数组中是否包含某个指定的字符串
查看>>
29、Java并发性和多线程-非阻塞算法
查看>>
安装OpenResty开发环境
查看>>
第0课 从0开始
查看>>
python class和class(object)用法区别
查看>>
hadoop无法启动DataNode问题
查看>>
java泛型中<?>和<T>区别
查看>>