题目来源:http://http://acm.nyist.net/JudgeOnline/problem.php?pid=17
单调递增最长子序列
题目思想很简单。方法也有很多。这只是其中的一种,比较容易理解罢了
经典的DP问题。时间复杂度是O(n^2)求最长上升子序列的长度。
#include<cstdio> #include<cstring> #include<iostream> using namespace std; #define N 10010 int main() { char str[N],a[N]; int i,j,len,T,count; scanf("%d",&T); while(T--) { scanf("%s",str); count=1; len=strlen(str); a[0]=0; for(i=0;i<len;i++) { for(j=count-1;j>=0;j--) { if(str[i]>a[j]) { a[j+1]=str[i]; if(j+1==count) count++; break; } } } printf("%d\n",count-1); } return 0; }