标签:print ios The bsp input put ring rom elements
Input
Output
Sample Input
abcfbc abfcab programming contest abcd mnp
Sample Output
4 2 0
题解:DP,最大公共子序列;dp[i][j]表示第一个串的第i个字符,第二个串的第j个字符所能匹配的最长公共子串。if s1[i]==s2[j] dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1])找最大值即可:
参考代码为:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1, str2;
while (cin >> str1 >> str2)
{
int l1 = str1.size();
int l2 = str2.size();
int dp[1010][1010]={0};
int Max = 0;
for (int i = 0; i<l1; i++)
{
for (int j = 0; j<l2; j++)
{
if (str1[i] == str2[j])
{
dp[i+1][j+1] = dp[i][j] + 1;
if (dp[i+1][j+1]>Max)
Max = dp[i+1][j+1];
}
else
{
dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]);
if (dp[i + 1][j + 1]>Max)
Max = dp[i + 1][j + 1];
}
}
}
cout << Max << endl;
}
return 0;
}
标签:print ios The bsp input put ring rom elements
原文地址:https://www.cnblogs.com/songorz/p/9409792.html