poj - 1458 - Common Subsequence(LCS)
2014-09-30 11:53
351 查看
题意:求两个字符串的最长公共子序列的长度。
题目链接:http://poj.org/problem?id=1458
——>>LCS模板题。。
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::max;
const int MAXN = 1000 + 10;
int dp[MAXN][MAXN];
int Lcs(string strA, string strB)
{
int nLenA = strA.length();
int nLenB = strB.length();
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= nLenA; ++i)
{
for (int j = 1; j <= nLenB; ++j)
{
if (strA[i - 1] == strB[j - 1])
{
dp[i][j] = dp[i - 1][j - 1] + 1;
}
else
{
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[nLenA][nLenB];
}
int main()
{
string strA;
string strB;
while (cin >> strA >> strB)
{
cout << Lcs(strA, strB) << endl;
}
return 0;
}
题目链接:http://poj.org/problem?id=1458
——>>LCS模板题。。
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::max;
const int MAXN = 1000 + 10;
int dp[MAXN][MAXN];
int Lcs(string strA, string strB)
{
int nLenA = strA.length();
int nLenB = strB.length();
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= nLenA; ++i)
{
for (int j = 1; j <= nLenB; ++j)
{
if (strA[i - 1] == strB[j - 1])
{
dp[i][j] = dp[i - 1][j - 1] + 1;
}
else
{
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[nLenA][nLenB];
}
int main()
{
string strA;
string strB;
while (cin >> strA >> strB)
{
cout << Lcs(strA, strB) << endl;
}
return 0;
}
相关文章推荐
- Poj 1458 Common Subsequence【LCS】【dp】
- HDU/HDOJ 1159/POJ 1458 Common Subsequence(最长公共子序列LCS,滚动数组)
- poj 1458 Common Subsequence(LCS)
- POJ 1458--Common Subsequence【LCS】
- POJ 1458 Common Subsequence(LCS)
- poj 1458 Common Subsequence --- LCS模板
- poj1458-Common Subsequence(最长公共子序列,LCS)
- POJ 1458 Common Subsequence 最长公共子序列(LCS)
- poj1458 Common Subsequence【LCS】
- POJ 1458-Common Subsequence(线性dp/LCS)
- HDU 1159 & POJ 1458 Common Subsequence 【LCS】
- 最长公共子序列(LCS, Longest Common Subsequence), POJ 1458
- poj 1458 Common Subsequence(LCS)
- POJ1458 Common Subsequence LCS问题入门题[DP]
- POJ1458 Common Subsequence LCS问题入门题[DP]
- HDU 1159 & POJ 1458 Common Subsequence(LCS 最长公共子序列O(nlogn))
- poj-1458-Common Subsequence(LCS)
- POJ-1458 Common Subsequence 【LCS模板】
- POJ 1458 Common Subsequence(最长公共子序列LCS)
- HDU1159 && POJ1458 Common Subsequence (LCS)