Note: Sub-sequences, which can be discontinuous;
The following topics, as I see it, are difficult to rank from easy to difficult:
Max and sub-sequences (Maximum sum subsequence)
This problem is purely entertaining ... There should be no such problem. The scenario is to put the sequence into a positive number.
vector<int> msseq (vector<int> &num) { vector<int> result; for (int i:num) if 0 ) result.push_back (i); return result;}
View Code
Maximum increment substring (longest increasing substring)
The violence programme TC is O (n^2). A better scheme is, the greedy method , set i from 0 to n traversal, with Gmax for the global longest length, Len represents the length of the current increment substring. When the increment is encountered, the len++ is encountered, the GMAX is updated, and the Len is reset to 1. TC = O (n), SC = O (1)
stringLisstringstr) { if(Str.empty ())returnstr; stringRecstr ="", Curstr =""; Curstr+ = str[0]; intGmax = int_min, Len =1; for(inti =1; I < str.length (); i++) { if(Str[i] > Str[i-1]) {len++; Curstr+=Str[i]; } Else { //assume that you only need to return one of the longest, and if you want to return all the vectors are stored if(Gmax <Len) {Gmax=Len; Recstr=Curstr; } curstr=""; Len=1; } } returnrecstr;}
View Code
Maximum increment subsequence (longest increasing subsequence)
The violence programme TC is O (2^n). If using the greedy method, the analysis know that according to the information contained in the first I can not cover the number of cases, so the decision of the number of i+1 cannot be done; a better scheme is, dynamic planning , with Opt[i] record to the longest and the longest increment subsequence containing the characters I have.
vector<int> Miseq (vector<int> &num) {Vector<int>result; if(Num.empty ())returnnum; Vector<int> Opt (num.size (),1), Record (Num.size (),-1); for(inti =0; I < num.size (); i++) { for(intj =0; J < I; J + +) { if(Num[i] > Num[j] && opt[j] +1>Opt[i]) {Opt[i]= Opt[j] +1; Record[i]=J; } } } intLast =-1, Gmax =0; for(inti =0; I < num.size (); i++) { if(Opt[i] >gmax) {Gmax=Opt[i]; Last=i; } } while(Last >=0) {result.push_back (num[last]); Last=Record[last]; } reverse (Result.begin (), Result.end ()); returnresult;}
View Code
Maximum no repetition string
Longest matching substring
Edit Distance
Max and sub-sequences
Maximum product sub-sequence
Maximum Product substring
[Classic] X (Long | Yamato | Large product) Y (sub-sequence | Sub-string)