-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1551C Interesting Story.cpp
79 lines (74 loc) · 2.14 KB
/
1551C Interesting Story.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
author : MishkatIT
created : Sunday 2023-08-20-19.46.10
*/
#include<bits/stdc++.h>
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define debug(_) cout << #_ << " is " << _ << '\n';
using namespace std;
using ll = long long;
using ld = long double;
const ll mod = 1e9 + 7;
const ll N = 1e5 + 10;
const ll inf = 1e9;
const ll linf = 1e18;
int main()
{
fio;
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
vector<pair<array<int, 5>, int>> v(n); // {occurrence of (a, b, c, d, e), total}
for (int i = 0; i < n; i++) {
string str;
cin >> str;
array<int, 5> freq;
freq.fill(0);
for (auto& x: str) {
freq[x - 'a']++;
}
int total = 0;
for (int x = 0; x < 5; x++) {
total += freq[x];
}
v[i] = {freq, total};
}
auto check = [&](int i) -> int {
sort(v.begin(), v.end(), [&](auto a, auto b)
{
int diff_a = a.first[i] - (a.second - a.first[i]);
int diff_b = b.first[i] - (b.second - b.first[i]);
if(diff_a == diff_b) {
// if(a.first[i] == b.first[i]) {
//// if(a.second == b.second) {
//// return true;
//// }
// return a.second < b.second;
// }
return a.first[i] > b.first[i];
}
return diff_a > diff_b;
});
int choosen = 0;
int rest = 0;
int cnt = 0;
for (auto& j: v)
{
if(choosen + j.first[i] > rest + j.second - j.first[i]) {
choosen += j.first[i];
rest += j.second - j.first[i];
cnt++;
}
}
return cnt;
};
int ans = 0;
for (int i = 0; i < 5; i++) {
ans = max(ans, check(i));
}
cout << ans << '\n';
}
return 0;
}