Problem:
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⋅10^5) — 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⋅10^5; the sum of the lengths of all words over all test cases doesn’t exceed 4⋅10^5.
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
input
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
output
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个字符串 要求输出能组成interesting故事的单词个数 interesting故事的要求:出现abcde中字母次数出现最多的要大于其余四个字母总的出现次数。
//比赛的时候一直没读懂题意…
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
char s[400005];
string str[200005];
int t, n, ans;
int a[200005];
int f(char c)
{
for (int i = 0; i < n; i++)
{
a[i] = 0;
}
for (int i = 0; i < n; i++)//假设当前字母是最大次数 求出每行字符串与当前字母次数的差
{
for (int j = 0; j < str[i].length(); j++)
{
if (str[i][j] == c)
{
a[i]++;
}
else
{
a[i]--;
}
}
}
sort(a,a+n);
reverse(a, a + n);//倒序排列
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += a[i];
if (sum <= 0)//如果其余字母出现的次数大于出现最多的字母次数
{
return i;
}
}
return n;
}
int main()
{
cin >> t;
while (t--)
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> s;
str[i] = s;
}
ans = 0;
for (int i = 0; i < 5; i++)
{
ans = max(ans, f('a' + i));
}
cout << ans << endl;
}
return 0;
}
本文介绍了一个算法问题,目标是找出由特定字母构成的单词序列中,能够形成“有趣故事”的最大单词数量。“有趣故事”定义为存在一个字母,在所有选定单词中出现次数超过其他所有字母总和。
1268

被折叠的 条评论
为什么被折叠?



