题解
O(n2) 和 O(nlogn) 两种解法。
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int t;
char dp[10005];
string src;
for(cin >> t; t--; )
{
cin >> src;
memset(dp, 0, sizeof(dp));
int n = src.length();
int len = 1;
dp[1] = src[0];
for(int i = 1; i < n; ++i)
{
if(src[i] > dp[len]) dp[++len] = src[i];
else{
int pos = lower_bound(dp + 1, dp + 1 + len, src[i]) - dp - 1;
dp[pos + 1] = src[i];
}
}
cout << len << endl;
/*for(int i = 0; i < n; ++i)
{
dp[i] = 1;
for(int j = 0; j < i; ++j)
if(src[i] > src[j]) dp[i] = max(dp[i], dp[j] + 1);
}
int ans = 0;
for(int i = 0; i < n; ++i)
ans = max(ans, dp[i]);
cout << ans << endl;*/
}
return 0;
}