动态规划 [子序列问题]

最长上升子序列
#include <iostream>
#include <string>
#include <math.h>
#include<algorithm>
using namespace std;
const int maxn = 1010;
int a[maxn]; int maxLen[maxn];
int main()
{
	int n; cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i]; maxLen[i] = 1;
	}
	for (int i = 2; i <= n; ++i)
	{
		for (int j = 1; j < i; ++j)
		{
			if (a[i] > a[j])
				maxLen[i] = max(maxLen[i], maxLen[j] + 1);
		}
	}
	cout << *max_element(maxLen + 1, maxLen + n + 1);
}
#include <iostream>
#include <string>
#include <math.h>
#include<algorithm>
using namespace std;
const int maxn = 1010;
int a[maxn]; int maxLen[maxn][maxn];
string s1, s2;
int main()
{
	while (cin >> s1 >>s2)
	{
		int length1 = s1.length();
		int length2 = s2.length();
		int ntemp;
		int i, j;
		for (i = 0; i <= length1; ++i)
		{
			maxLen[i][0] = 0;
		}
		for (j = 0; j <= length2; ++j)
		{
			maxLen[0][j] = 0;
		}
		for (i = 1; i <= length1; ++i)
			for (j = 1; j <= length2; ++j)
				if (s1[i] == s2[j])
					maxLen[i][j] = maxLen[i - 1][j - 1] + 1;
				else
					maxLen[i][j] = max(maxLen[i][j - 1], maxLen[i - 1][j]);
		cout << maxLen[length1][length2] << endl;
	}
}
posted @ 2020-08-24 22:14  _Hsiung  阅读(39)  评论(0编辑  收藏  举报