HDU 1159 Common Subsequence 【最长公共子序列】模板题

<题目链接>

题目大意:
给出两个字符串,求其最长公共子序列的长度。

模板题。

#include <bits/stdc++.h>
using namespace std;

const int N = 1e3+5;
int dp[N][N];
string s1,s2;

int main(){
    while(cin>>s1>>s2){
        int L1=s1.length();
        int L2=s2.length();
        for(int i=0;i<=L1;i++){
            for(int j=0;j<=L2;j++){
                if(i==0 || j==0){ dp[i][j]=0;continue; }
                if(s1[i-1]==s2[j-1]) dp[i][j]=dp[i-1][j-1]+1;
                else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            }
        }
        printf("%d\n",dp[L1][L2]);
    }
}

 

posted @ 2018-04-29 20:23  悠悠呦~  阅读(200)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end