题目:https://codeforces.com/contest/1551/problem/C
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
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
又是一道简单题,看得懂题,写出来的程序还是超时(想到方法的时候其实算过复杂度,带着侥幸心理希望能过)。。。。还是想不到更优的算法。。只会看题解。。
题目大意:
根据问题要求,要我们求在多个单词里面出现最多的字母个数比这些单词其他字母加起来的个数还多的最多单词个数
解题思路:
计算每个单词里面的字母在当前单词里面与当前单词其他字母和的差,每个单词都是一样的操作,每个单词每个不同字母都用对应的数组存起来,最后按降序排序就可以求出来最大结果,具体看代码
AC代码:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> b[5]; //题目说了只用五个字母
int main() {
ios_base::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n; cin >> n;
string s;
for (int i = 0; i < 5; i++)b[i].clear();
for (int i = 1; i <= n; i++) {
cin >> s;
int len = -int(s.size());
for (int j = 0; j < 5; j++)
b[j].push_back(len);
for (auto c : s) {
b[c - 'a'].back() += 2;
}
}
int bestBalance = 0;
int bestCount = 0;
for (int i = 0; i < 5; i++) {
auto& t = b[i];
sort(t.begin(), t.end());
reverse(t.begin(), t.end());
int temp = t[0];
int j;
for (j = 1; j < n && temp > 0; j++) {
temp += t[j];
}
if (temp <= 0) j--;
if (j > bestCount) {
bestCount = j;
}
}
cout << bestCount << endl;
}
return 0;
}