题目1042:Coincidence

来源:互联网 发布:淘宝导入csv上架流程 编辑:程序博客网 时间:2024/06/06 20:54

题目1042:Coincidence

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:1672

解决:886

题目描述:

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>#include<algorithm>#include<memory.h>using namespace std;int main(){        string X,Y;        int dp[110],x,y,temp1,temp2;        while(cin>>X>>Y){               memset(dp,0,sizeof(dp));               for(x=1;x<=X.length();++x){                       temp1=temp2=0;                       for(y=1;y<=Y.length();y++){                              temp2=dp[y];                              if(X[x-1]==Y[y-1]) dp[y]=temp1+1;                              else        dp[y]=max(dp[y],dp[y-1]);                              temp1=temp2;                      }                        }            cout<<dp[Y.length()]<<endl;      }}


0 0