【C/C++】最长不下降子序列/动态规划

#include <iostream>
#include <vector>

using namespace std;

int main() 
{
   //输入
   int tmp;
   vector<int> input;
   while (cin >> tmp)
   {
      input.push_back(tmp);
      if (cin.get() == '
')
      {
         break;
      }
   }

   for (vector<int>:: iterator it = input.begin(); it != input.end(); it++)
   {
      cout << *it << endl;
   }

   //序列input,设dp[i]是A[i]结尾的最长不下降子序列的长度
   int n = input.size(); 
   cout << "n = " << n << endl;

   vector<int> dp;
   
   int ans = -1;

   for (int i = 0; i < n; i++)
   {
      dp.push_back(1);
      for (int j = 0; j < i; j++)
      {
         if ((input[j] <= input[i]) && (dp[j]+1 > dp[i]))
         {
            dp[i] = dp[j] + 1;
         }
      }
      ans = max(ans, dp[i]);
   }

   cout << ans << endl;
   

   system("pause");
}

原文地址:https://www.cnblogs.com/kinologic/p/14538046.html