问题 D: Coincidence
时间限制: 1 Sec 内存限制: 32 MB
提交: 143 解决: 72
[提交][状态][讨论版][命题人:外部导入]
题目描述
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.
样例输入
google goodbye
样例输出
4
本题为最长公共子序列
最长公共子序列主要内容:
#include<bits/stdc++.h>
using namespace std;
const int N=105;
char a[N],b[N];
int dp[N][N];
int main(){
int n;
while(cin>>a+1>>b+1){
fill(dp[0],dp[0]+N*N,0);
int lenA=strlen(a+1);
int lenB=strlen(b+1);
for(int i=0;i<=lenA;i++) dp[i][0]=0;
for(int i=0;i<=lenB;i++) dp[0][i]=0;
for(int i=1;i<=lenA;i++){
for(int j=1;j<=lenB;j++){
if(a[i]==b[j]){
dp[i][j]=dp[i-1][j-1]+1;
}else{
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
}
}
}
cout<<dp[lenA][lenB]<<endl;
}
return 0;
}