zoukankan      html  css  js  c++  java
  • 动态规划 [子序列问题]

    最长上升子序列
    #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;
    	}
    }
    
  • 相关阅读:
    20151104内置对象
    20151102adonet2
    20151029adonet1
    20151028c#4
    20151027c#3
    20151026c#2
    20151021c#1
    20151020sql2
    20151019sql1
    Codeforces Round #261 (Div. 2) C. Pashmak and Buses(思维+构造)
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13811935.html
Copyright © 2011-2022 走看看