本文主要是介绍九度oj-1042-Coincidence,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 题目描述:
-
Find a longest common subsequence of two strings.
- 输入:
-
First and second line of each input case contain two strings of lowercase character a…z. There are no spaces before, inside or after the strings. Lengths of strings do not exceed 100.
- 输出:
-
For each case, output k – the length of a longest common subsequence in one line.
- 样例输入:
-
abcd cxbydz
- 样例输出:
-
2
- 来源:
- 2008年上海交通大学计算机研究生机试真
-
-
#include<iostream> #include<cstring> #include<algorithm> using namespace std; int a[103][103]; string b,c; int main() {int i,j;while(cin>>b>>c){int bb=b.length();int cc=c.length();memset(a,0,sizeof(a));if(b[0]==c[0]) a[0][0]=1;for(i=1;i<bb;i++)if(b[i]==c[0]) a[i][0]=1;else a[i][0]=a[i-1][0];for(j=1;j<cc;j++)if(c[j]==b[0]) a[0][j]=1;else a[0][j]=a[0][j-1];for(i=1;i<bb;i++)for(j=1;j<cc;j++){if(b[i]==c[j])a[i][j]=a[i-1][j-1]+1;elsea[i][j]=max(a[i-1][j],a[i][j-1]);}cout<<a[bb-1][cc-1]<<endl;}return 0; }
这篇关于九度oj-1042-Coincidence的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!