题目描述
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
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 110;
char A[N],B[N];
int dp[N][N];
int main(){
int n;
while(scanf("%s",A+1)!=EOF){
scanf("%s",B+1);
int lenA = strlen(A+1),lenB = strlen(B+1);
for(int i = 0;i <= lenA;i++){
dp[i][0] = 0;
}
for(int j = 0;j <= lenB;j++){
dp[0][j] = 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 -1][j],dp[i][j -1]);
}
}
}
printf("%d\n",dp[lenA][lenB]);
}
return 0;
}
/**************************************************************
Problem: 1132
User: 2015212040209
Language: C++
Result: 正确
Time:0 ms
Memory:1164 kb
****************************************************************/