题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159

Common Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18201    Accepted Submission(s): 7697

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
 

解题思路:

开一个二维数组f[N][M]记录第一个数组取前N个数和第二个数组取前M个数的时候公共子序列的最大长度,最后输出f[l1][l2]即为最后结果。

 
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char keng1[],keng2[];
int con[][];
int getmax(int a,int b)
{if(a>b)return a;else return b;}
int main()
{
int i,j,l1,l2,Max;
keng1[]=keng2[]='#';
while(scanf("%s%s",keng1+,keng2+)!=EOF)
{
Max=;
l1=strlen(keng1);
l2=strlen(keng2);
memset(con,,sizeof(con));
for(i=;i<l1;i++)
{
for(j=;j<l2;j++)
{
if(keng1[i]==keng2[j])
con[i][j]=getmax(con[i][j],con[i-][j-]+);
else
con[i][j]=getmax(con[i-][j],con[i][j-]);
}
}
printf("%d\n",con[l1-][l2-]);
}
return ;
}
 
 

最新文章

  1. ASP.NET(IIS)出现&quot;没有为请求类型&quot;GET&quot;找到 HTTP 处理程序&quot;
  2. OAF_开发系列11_实现OAF通过DataBoundValues动态显示表列的左右对齐
  3. sql in(1,2,3)参数化查询,错误在将 varchar 值 &#39;1,2,3,4&#39; 转换成数据类型 int 时失败
  4. Brn系列商城4.1正式发布,欢迎大家下载体验
  5. {$ecs_css_path}
  6. git gc
  7. hdu1025 Constructing Roads In JGShining&amp;#39;s Kingdom (nlogn的LIS)
  8. phpstorm集成phpunit
  9. 如何把一个TXT文本文件按行数分割成多个文本文件
  10. ARM开发(1) 基于STM32的LED跑马灯
  11. Treap(树堆)
  12. c#控制WPF程序自动登录(Automation方式实现)
  13. dva-quickstart 与 create-react-app 比较(一)
  14. Compass 更智能的搜索引擎(1)--入门
  15. 今天读一读七天学会NodeJS
  16. 同事问如何判断同花顺,我用javascript的二维数组写了个简易demo
  17. 一名全栈设计师的Mac工具箱(设计,开发,效率)
  18. 背包问题(dp基础)
  19. 流畅的python第十五章上下文管理器和else块学习记录
  20. 异步消息postEvent更新界面

热门文章

  1. CI的面向切面的普通权限验证
  2. 分享自己动手弄的基于Rime的新世纪五笔输入法码表
  3. python 循环while和for in
  4. C++ 11 笔记 (三) : auto
  5. Highcharts 本地导出图片和PDF asp.net mvc版
  6. 加密算法 - RSA算法二
  7. App - 版本控制
  8. QT窗口渐现效果,窗口震动效果,鼠标移动窗口
  9. Word删除空白页
  10. UserMailbox 必须强制使用 Database---Database is mandatory on UserMailbox error