题目大意:求出两个给定字符串的最大上升子序列
解题思路:状态转移方程,如果a[i-1] = b[i-1],那么dp[i][j] = dp[i-1][j-1]+1,否则dp[i][j] = max(dp[i-1][j],dp[i][j-1]),在dp前记得初始化dp[i][0]与dp[0][j]为0,递推计算从1到字符串长度
题目坑点:字符串可能包含空格,需要用getline输入
代码如下:
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int类型最大值INT_MAX,short最大值为SHORT_MAX
long long最大值为LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x) memset(x,0,sizeof(x))
using namespace std;
const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
string a,b;
int dp[1010][1010];
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
while(getline(cin,a))
{
getline(cin,b);
_clr(dp);
int x = a.length(),y = b.length();
for(int i = 1;i<=x;i++)
for(int j = 1;j<=y;j++)
{
if(a[i-1] == b[j-1]) dp[i][j] = dp[i-1][j-1]+1;
else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
cout << dp[x][y] << endl;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}