HDU 5842 Lweb and String(Lweb与字符串)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description - 题目描述
Lweb has a string S.
Oneday, he decided to transform this string to a new sequence.
You need help him determine this transformation to get a sequence which has the longest LIS(Strictly Increasing).
You need transform every letter in this string to a new number.
A is the set of letters of S, B is the set of natural numbers.
Every injection f:A→B can be treat as an legal transformation.For example, a String “aabc”, A={a,b,c}, and you can transform it to “1 1 2 3”, and the LIS of the new sequence is 3.Now help Lweb, find the longest LIS which you can obtain from S.
LIS: Longest Increasing Subsequence. (https://en.wikipedia.org/wiki/Longest_increasing_subsequence)
Lweb有个字符串S 某天,他决定用原串弄腾出一个新串。 你需要帮他确定变化的形式,使得新串具有最长的LIS(严格递增)。 你需要把字符串中的每种字母转化成一个数字。 A是S的字母集合,B是自然数集合。 任意映射 f:A→B 都是合法变动。 比如说,一个字符串“aabc”,A={a,b,c},你可以转换成“1 1 2 3”,新串的LIS长度为3。 现在帮帮Lweb找出你能从S中获得的最长LIS。 LIS:最长升序子序列。(https://en.wikipedia.org/wiki/Longest_increasing_subsequence)
Input - 输入
The first line of the input contains the only integer T,(1≤T≤20).
Then T lines follow, the i-th line contains a string S only containing the lowercase letters, the length of S will not exceed 105.
输入的第一行只有一个整数T,(1≤T≤20)。 随后T行,每行都有一个仅由小写字母组成的字符串S,S的长度不超过10^5。
Output - 输出
For each test case, output a single line "Case #x: y", where x is the case number, starting from 1. And y is the answer.
对于每组测试用例,输出一行"Case #x: y",x表示测试用例的编号,从1开始。y为答案。
Sample Input - 输入样例
2 aabcc acdeaa
Sample Output - 输出样例
Case #1: 3 Case #2: 4
题解
字母转数字,然后求最长升序子序列的长度。
因此映射关系是随意自定的,因此题目只要求字符串中的字母种类数量即可。
代码 C++
1 #include <cstdio> 2 #include <cstring> 3 char data[100005], inUS[128]; 4 int main(){ 5 int t, i, j, opt; 6 scanf("%d ", &t); 7 for (i = 1; i <= t; ++i){ 8 gets(data); 9 memset(inUS, 0, sizeof(inUS)); opt = 0; 10 for (j = 0; data[j]; ++j){ 11 if (~inUS[data[j]]) ++opt, --inUS[data[j]]; 12 } 13 printf("Case #%d: %d ", i, opt); 14 } 15 return 0; 16 }