題目1042:Coincidence

題目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.

樣例輸入:
abcd
cxbydz
樣例輸出:
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;
      }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章