九度oj-1042-Coincidence

来源:互联网 发布:查看iptables开放端口 编辑:程序博客网 时间:2024/05/20 03:41

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:2338

解决:1255

题目描述:

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
来源:
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;            else                a[i][j]=max(a[i-1][j],a[i][j-1]);        }        cout<<a[bb-1][cc-1]<<endl;    }    return 0;}


0 0
原创粉丝点击