Source
Typical questions
Classic questions of dynamic planning; there seem to be several solutions. I am studying the most basic solutions;
Here I directly referred to the http://blog.csdn.net/sjf0115/article/details/8715672 blog, copy the picture here, I feel that it is not bad, deepen the understanding of this kind of questions;
Http://www.cnblogs.com/mycapple/archive/2012/08/22/2651453.html, this blog is also good
# Include <cstdio> # include <cstring> const int maxn = 10001; char s [maxn]; int DP [maxn], Max; void LICs () {int Len; memset (DP, 0, sizeof (DP); Len = strlen (s); For (INT I = 0; I <Len; I ++) {DP [I] = 1; // when an array is given, the initial value is 1. The maximum sequence of an array must have one character; For (Int J = 0; j <I; j ++) {If (s [I]> S [J] & DP [I] <1 + dp [J]) // recursive formula, if this position is greater than the previous character, add it to the incremental sequence DP [I] = 1 + dp [J] ;}} max = 0; for (INT I = 0; I <Len; I ++) // obtain the maximum value if (max <DP [I]) max = DP [I];} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% s", S); LICs (); printf ("% d \ n", max);} return 0 ;}
I saw the optimal code for this question. I wrote more than 300 ms for submission, and the optimal code can be up to 4 ms and 0 ms can be achieved;
But it's a bit difficult to understand. Save and learn;
#include<iostream>#include <string>//#include <time.h>using namespace std;int main(){//freopen("1.txt","r",stdin);int n ;cin>>n;while(n--){string str;int count=1;cin>>str;int a[200];a[0]=-999;for (int i=0;i<str.length();i++){for (int j=count-1;j>=0;j--){if((int)str[i]>a[j]){a[j+1]=str[i];if(j+1==count) count++;break;}}}cout<<count-1<<endl;}//cout<<(double)clock()/CLOCKS_PER_SEC<<endl;return 0;}