每日一九度之 题目1042:Coincidence

时间:2022-04-07 09:43:44

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3007

解决:1638

题目描述:

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

经典的dp问题。LCS。相当于dp的入门级题目吧!

//Asimple
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <limits.h>
#define INF 0x7fffffff
using namespace std;
const int maxn = ;
typedef long long ll;
int n, num;
char s1[maxn], s2[maxn];
int dp[maxn][maxn]; int main(){
while( cin >> s1 >> s2 ){
int len1 = strlen(s1);
int len2 = strlen(s2);
for (int i=; i<=len1; ++i)
dp[i][] = ;
for (int j=; j<=len2; ++j)
dp[][j] = ;
for(int i=; i<=len1; i++){
for(int j=; j<=len2; j++){
if( s1[i-] == s2[j-] ){
dp[i][j] = dp[i-][j-] + ;
} else {
dp[i][j] = max(dp[i-][j],dp[i][j-]);
}
}
}
printf("%d\n",dp[len1][len2]);
}
return ;
}