poj 1458 Common Subsequence(lcs模板)
2015-08-10 17:58
417 查看
Description
Asubsequenceofagivensequenceisthegivensequencewithsomeelements(possiblenone)leftout.GivenasequenceX=<x1,x2,...,xm>anothersequenceZ=<z1,z2,...,zk>isasubsequenceofXifthereexistsastrictly
increasingsequence<i1,i2,...,ik>ofindicesofXsuchthatforallj=1,2,...,k,xij=zj.Forexample,Z=<a,b,f,c>isasubsequenceofX=<a,b,c,f,b,c>withindexsequence<1,2,4,6>.GiventwosequencesX
andYtheproblemistofindthelengthofthemaximum-lengthcommonsubsequenceofXandY.
Input
Theprograminputisfromthestdinput.Eachdatasetintheinputcontainstwostringsrepresentingthegivensequences.Thesequencesareseparatedbyanynumberofwhitespaces.Theinputdataarecorrect.
Output
Foreachsetofdatatheprogramprintsonthestandardoutputthelengthofthemaximum-lengthcommonsubsequencefromthebeginningofaseparateline.
SampleInput
SampleOutput
Asubsequenceofagivensequenceisthegivensequencewithsomeelements(possiblenone)leftout.GivenasequenceX=<x1,x2,...,xm>anothersequenceZ=<z1,z2,...,zk>isasubsequenceofXifthereexistsastrictly
increasingsequence<i1,i2,...,ik>ofindicesofXsuchthatforallj=1,2,...,k,xij=zj.Forexample,Z=<a,b,f,c>isasubsequenceofX=<a,b,c,f,b,c>withindexsequence<1,2,4,6>.GiventwosequencesX
andYtheproblemistofindthelengthofthemaximum-lengthcommonsubsequenceofXandY.
Input
Theprograminputisfromthestdinput.Eachdatasetintheinputcontainstwostringsrepresentingthegivensequences.Thesequencesareseparatedbyanynumberofwhitespaces.Theinputdataarecorrect.
Output
Foreachsetofdatatheprogramprintsonthestandardoutputthelengthofthemaximum-lengthcommonsubsequencefromthebeginningofaseparateline.
SampleInput
abcfbcabfcab programmingcontest abcdmnp
SampleOutput
4 2 0
#include<stdio.h>
#include<string.h>
chars1[1010],s2[1010];
intdp[1010][1010];//开一个dp数组用来存储最长子串的长度;
intmax(inta,intb)
{
if(a>b)
returna;
elsereturnb;
}
//#definemax(a,b)(a>b?a:b)这样比较两个数大小比较简单
intmain()
{
while(scanf("%s%s",&s1,&s2)!=EOF)
{
memset(dp,0,sizeof(dp));
intlen1=strlen(s1);
intlen2=strlen(s2);
inti,j;
for(i=1;i<=len1;i++)
{
for(j=1;j<=len2;j++)
{
if(s1[i-1]==s2[j-1])//如果最后一位字符一样就+1
{
dp[i][j]=dp[i-1][j-1]+1;
}
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);//不是的话就取最大的继续循环
}
}
printf("%d\n",dp[len1][len2]);
}
return0;
}
相关文章推荐
- StringBuilder操作
- String、StringBuilder、StringBuffer
- C# SortedDictionary<TKey, TValue> 类
- 什么是优先级队列(priority queue)?
- iOS7下 UISearchBar设置ReturnKeyType
- POJ 2031 && ZOJ 1718--Building a Space Station【最小生成树 && kurskal && 水题】
- StringBuilder,String, int转为String
- UITableVIew 滚动流畅性优化
- 提高duilib的richedit控制的一些特征
- iOS-关于UINavigationController的title不显示问题
- poj1458 Common Subsequence【LCS】
- Android多国语言的value文件夹命名方式
- Android ContentValues类
- HDU 5273 Dylans loves sequence——BestCoder Round #45(DP or 树状数组)
- 使用phpQuery 抓取HTML 页面内容
- easyui中的panel(面板)的属性href的使用
- zoj 3545 - Rescue the Rabbit(AC自动机+dp)
- POJ_1679_The Unique MST(次小生成树模板)
- codeforces 527D D. Clique Problem(二分+线段树+贪心+dp)
- 控件学习---UIImageView---摘自培训文档