網頁

2013年8月3日 星期六

c001: 最長共同字串(LCS)、UVA 10405 - Longest Common Subsequence

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#define MAX 1001
using namespace std;

int main(){
    string s1, s2 ;
    int LCS_table[MAX][MAX] ;
    while(cin>>s1>>s2){
        memset(LCS_table,0,sizeof(LCS_table));
        for(int i=1 ; i<s1.length()+1 ; ++i){
            for(int j=1 ; j<s2.length()+1 ; ++j){
                if(s1[i-1] == s2[j-1]){
                    LCS_table[i][j] = LCS_table[i-1][j-1]+1 ;
                }
                else{
                    LCS_table[i][j] = max(LCS_table[i-1][j],LCS_table[i][j-1]) ;
                }
            }
        }
        cout << LCS_table[s1.length()][s2.length()] << endl ;
    }
    return 0 ;
}

沒有留言:

張貼留言