Interesting Story

原地址
题目:C. Interesting Story
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Stephen Queen wants to write a story. He is a very unusual writer, he uses only letters ‘a’, ‘b’, ‘c’, ‘d’ and ‘e’!

To compose a story, Stephen wrote out n words consisting of the first 5 lowercase letters of the Latin alphabet. He wants to select the maximum number of words to make an interesting story.

Let a story be a sequence of words that are not necessarily different. A story is called interesting if there exists a letter which occurs among all words of the story more times than all other letters together.

For example, the story consisting of three words “bac”, “aaada”, “e” is interesting (the letter ‘a’ occurs 5 times, all other letters occur 4 times in total). But the story consisting of two words “aba”, “abcde” is not (no such letter that it occurs more than all other letters in total).

You are given a sequence of n words consisting of letters ‘a’, ‘b’, ‘c’, ‘d’ and ‘e’. Your task is to choose the maximum number of them to make an interesting story. If there’s no way to make a non-empty story, output 0.

Input
The first line contains one integer t (1≤t≤5000) — the number of test cases. Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤2⋅105) — the number of the words in the sequence. Then n lines follow, each of them contains a word — a non-empty string consisting of lowercase letters of the Latin alphabet. The words in the sequence may be non-distinct (i. e. duplicates are allowed). Only the letters ‘a’, ‘b’, ‘c’, ‘d’ and ‘e’ may occur in the words.

It is guaranteed that the sum of n over all test cases doesn’t exceed 2⋅105; the sum of the lengths of all words over all test cases doesn’t exceed 4⋅105.

Output
For each test case, output the maximum number of words that compose an interesting story. Print 0 if there’s no way to make a non-empty interesting story.
Example
inputCopy
6
3
bac
aaada
e
3
aba
abcde
aba
2
baba
baba
4
ab
ab
c
bc
5
cbdca
d
a
d
e
3
b
c
ca
outputCopy
3
2
0
2
3
2
Note
In the first test case of the example, all 3 words can be used to make an interesting story. The interesting story is “bac aaada e”.

In the second test case of the example, the 1-st and the 3-rd words can be used to make an interesting story. The interesting story is “aba aba”. Stephen can’t use all three words at the same time.

In the third test case of the example, Stephen can’t make a non-empty interesting story. So the answer is 0.

In the fourth test case of the example, Stephen can use the 3-rd and the 4-th words to make an interesting story. The interesting story is “c bc”.

题目大意

输入n个单词,他们都是由a,b,c,d,e,f所构成的。如果由其中的maxx个单词组成的文章中一个字母出现的次数,比其他所以字母出现的次数和都多,则称这个文章为有趣的。判断maxx的最大值。

思路

先把n个单词读入,对于5个字母
我们依次遍历一下所有单词,并记录每个单词对这个字母的贡献值是多少(贡献值的计算,当这个字母与我们讨论的那个最多的字母相同时,他的贡献值为1,其他时候,贡献值为-1)
将贡献值逆序排列,并不断求和,当 这个和值<=0时,即是不在满足,去最大的单词个数,即贡献值加了多少个
ac代码

#include <bits/stdc++.h>
using namespace std;

#define pb push_back

int32_t main() {
    int t,n,ans = 0;
    cin>>t;
    while(t--){
        ans = 0;
        cin>>n;
        vector<int > val(n);
        int dif=0;
        vector<string> s(n);
        for(int i =0 ;i <n;i++){
            cin>>s[i];
        }
        for(auto i:{'a','b','c','d','e'}){
            val.clear();
            for(auto j:s){
                dif = 0;
                for(auto k:j){
                    if(k==i)dif++;
                    else dif--;
                }
                val.pb(dif);
            }
            sort(val.rbegin(),val.rend());
            int sum = 0;
            for (int j = 0; j < n; ++j) {
                sum += val[j];
                if(sum <= 0) {
                    ans = max(j,ans);break;
                }
                if(j==n-1) ans = max(n,ans);
            }
        }
        cout << ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夭辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值