1961 Problem D Coincidence

问题 D: Coincidence

时间限制: 1 Sec  内存限制: 32 MB
提交: 44  解决: 21
[提交][状态][讨论版][命题人:外部导入]

题目描述

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<iostream>
#include<string>
#include<algorithm>
using namespace std;
//同最长公共子序列
//dp[i][j]表示字符串a的i号位和字符串b的j号位之前的LCS长度
//LCS:(Longest Common Subsequence)
int main() {
	string a, b;
	while (cin >> a >> b) {
		int dp[110][110] = { 0 };
		a = " " + a, b = " " + b;
		for (int i = 1; i < a.length(); i++) {
			for (int j = 1; j < b.length(); 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]);
			}
		}
		cout << dp[a.length() - 1][b.length() - 1] << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36502291/article/details/84778557