题目1042:Coincidence

来源:互联网 发布:淘宝对话生成器 编辑:程序博客网 时间:2024/06/06 03:26
题目描述:

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.

样例输入:
abcdcxbydz
样例输出:

2

C++代码:

#include<iostream>#include<string>using namespace std;int a[110][110];string s1,s2;int main(){    while(cin>>s1>>s2){        int m=s1.length();        int n=s2.length();        for(int i=0;i<m;i++)          for(int j=0;j<n;j++){                a[i][j]=0;          }        for(int i=1;i<=m;i++)        for(int j=1;j<=n;j++){            if(s1.at(i-1)==s2.at(j-1)){                a[i][j]=a[i-1][j-1]+1;            }else{                if(a[i-1][j]>=a[i][j-1]){                    a[i][j]=a[i-1][j];                }else{                    a[i][j]=a[i][j-1];                }            }        }        cout<<a[m][n]<<endl;    }    return 0;}


原创粉丝点击