博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最长公共子序列问题
阅读量:3948 次
发布时间:2019-05-24

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

最长公共子序列问题

Time Limit: 1000 ms Memory Limit: 65536 KiB

 

Problem Description

给定两个序列 X={x1,x2,…,xm} 和 Y={y1,y2,…,yn},找出X和Y的最长公共子序列。

Input

输入数据有多组,每组有两行 ,每行为一个长度不超过500的字符串(输入全是大写英文字母(A,Z)),表示序列X和Y。

Output

每组输出一行,表示所求得的最长公共子序列的长度,若不存在公共子序列,则输出0。

Sample Input

ABCBDABBDCABA

Sample Output

4

代码如下:

#include 
#include
#include
int main(){ char X[500],Y[510]; int c[510][510]= {0,}; while(scanf("%s%s",X,Y)!=EOF) { int i,j,x,y; x=strlen(X); y=strlen(Y); for(i=0; i<=x; i++) { c[i][0]=0; } for(j=0; j<=y; j++) { c[0][j]=0; } for(i=1; i<=x; i++) { for(j=1; j<=y; j++) { if(X[i-1]==Y[j-1]) { c[i][j]=c[i-1][j-1]+1; } else { if(c[i-1][j]>c[i][j-1]) { c[i][j]=c[i-1][j]; } else c[i][j]=c[i][j-1]; } } } printf("%d\n",c[x][y]); } return 0;}

 

转载地址:http://dkhwi.baihongyu.com/

你可能感兴趣的文章
Providing Descendant and Lateral Navigation 提供下一代和横向导航
查看>>
GPS 0183协议GGA、GLL、GSA、GSV、RMC、VTG解释 + 数据解析
查看>>
android如何使得电阻屏在第一次开机时自动叫起屏幕校准程序
查看>>
android如何实现:当开启图案解锁时,取消滑动解锁
查看>>
Providing Ancestral and Temporal Navigation 设计高效的应用导航
查看>>
Putting it All Together: Wireframing the Example App 把APP例子用线框图圈起来
查看>>
Implementing Lateral Navigation 实现横向导航
查看>>
Implementing Ancestral Navigation 实现原始导航
查看>>
Implementing Temporal Navigation 实现时间导航
查看>>
Responding to Touch Events 响应触摸事件
查看>>
Defining and Launching the Query 定义和启动查询
查看>>
Handling the Results 处理结果
查看>>
如何内置iperf到手机中
查看>>
如何adb shell进入ctia模式
查看>>
Contacts Provider 联系人存储
查看>>
android 图库播放幻灯片时灭屏再亮屏显示keyguard
查看>>
android 图库语言更新
查看>>
android camera拍照/录像后查看图片/视频并删除所有内容后自动回到camera预览界面
查看>>
android 图库中对非mp4格式的视频去掉"修剪"功能选项
查看>>
how to disable watchdog
查看>>