-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution_bf.cpp
More file actions
48 lines (39 loc) · 1.06 KB
/
solution_bf.cpp
File metadata and controls
48 lines (39 loc) · 1.06 KB
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
if (!(cin >> n)) {
return 0;
}
string s;
cin >> s;
long long calmCount = 0;
// Brute force: enumerate all substrings [l..r] and check calmness by counting frequencies.
for (int l = 0; l < n; l++) {
for (int r = l; r < n; r++) {
int m = r - l + 1;
vector<int> freq(26, 0);
for (int i = l; i <= r; i++) {
freq[s[i] - 'a']++;
}
bool isCalm = true;
// A substring is calm iff no letter occurs strictly more than m/2 times.
// Avoid floating-point: check 2*freq <= m for every letter.
for (int c = 0; c < 26; c++) {
if (2 * freq[c] > m) {
isCalm = false;
break;
}
}
if (isCalm) {
calmCount++;
}
}
}
cout << calmCount;
return 0;
}