九度oj 1042

来源:互联网 发布:html 作为java gui 编辑:程序博客网 时间:2024/05/17 07:36
题目描述:

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<string.h>#include<stdio.h>using namespace std;int a[101][101];int max(int x,int y){    return (x>y)?x:y;}int main(){    char s1[101],s2[101];    while(cin>>s1>>s2)    {                      int l1=strlen(s1);                      int l2=strlen(s2);                      for(int i=0;i<=l1;i++)                      {                              a[i][0]=0;                              }                              for(int j=0;j<=l2;j++)                              {                                      a[0][j]=0;                                      }                                      for(int i=1;i<=l1;i++)                                      {                                              for(int j=1;j<=l2;j++)                                              {                                                      if(s1[i-1]!=s2[j-1])                                                      {                                                                          a[i][j]=max(a[i][j-1],a[i-1][j]);                                                                                                                                                    }                                                                          else                                                                          a[i][j]=a[i-1][j-1]+1;                                                                          }                                                                          }                                                                          cout<<a[l1][l2]<<endl;                                                                          }                                                                          }

0 0
原创粉丝点击