1007 CCPC Training Class

http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1007&cid=909

CCPC Training Class

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 5145    Accepted Submission(s): 2911

Problem Description
Baby volcano is helping his CCPC coach preparing a new CCPC training contest. He wants to generate high quality data on border tree(There is no need to know what border tree is), but he encounters some troubles. Could you please help him?

In this paragraph we formally define the data quality problem. For some string s=s1s2s3sn, we use s[l:r] to denote the substring starts from l and ends at r, if l>rs[l:r] is empty. We further define:
Lborderi=max{0j<i | s[1:j]=s[ij+1:i]}

to denote the longest border at some position i, then we define D(i) to denote the length of border chain at position i:
D(i)={0D(LBorderi)+1i=0i>0

The quality W of this string is define as the maximum of D:
W=maxi=0nD(i)


To distinguish border tree with naive brute force algorithms, baby volcano need to generate strings such that its quality W is as large as possible.

Now given a string s, you could permute s arbitrarily. What is the maximum quality W you could reach after permuting s?
 

Input

In the first line there is a number T(T20), denotes the number of test cases.
In the next T lines, for each line there is a string s(1|s|105), denotes the input string, all inputs are formed in lowercases.
 

Output

Output T lines, for each line, you need to output ''Case #t: m''(without quotes), where t is the index of this test case, m is the maximum quality you could reach after permuting.
 

Sample Input

5
abcde
sankaranarayanan
abbccaabc
programming
monotone
 
Sample Output
Case #1: 1
Case #2: 7
Case #3: 3
Case #4: 2
Case #5: 3
 
思路:输出出现次数最多的字母的出现次数
代码:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

int T;
string str;
vector<int> ans;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> T;
    for (int i = 1; i <= T; ++i)
    {
        ans.resize(26, 0);
        cin >> str;
        for (int j = 0; j < str.length(); ++j)
        {
            ans[str[j] - 'a']++;
        }
        sort(ans.begin(), ans.end());
        cout << "Case #" << i << ": " << ans[25] << '
';
        ans.clear();
    }
    return 0;
}

  

 
 

原文地址:https://www.cnblogs.com/SutsuharaYuki/p/13718270.html