-
Notifications
You must be signed in to change notification settings - Fork 0
/
1984D.cpp
130 lines (105 loc) · 2.49 KB
/
1984D.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#ifdef DBG
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#else
#define dbg(...)
#endif
const int MAXN = 200005; // Adjust as needed
// Global variables to store input data
vector<string> test_cases;
void reset_globals() {
// Clear or reset global data
test_cases.clear();
}
vector<int> z_function(const string &s) {
int n = s.size();
vector<int> z(n, 0);
int l = 0, r = 0;
z[0] = n;
for (int i = 1; i < n; ++i) {
if (i <= r) {
z[i] = min(r - i + 1, z[i - l]);
}
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) {
++z[i];
}
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
return z;
}
ll count_nonempty_strings(const string &s) {
int n = s.length();
vector<int> to_not_a(n);
for (int i = n - 1; i >= 0; i--) {
if (s[i] == 'a') {
to_not_a[i] = i + 1 < n ? to_not_a[i + 1] : n;
} else {
to_not_a[i] = i;
}
}
if (to_not_a[0] == n) {
dbg("all a\n");
return n - 1;
}
int i0 = to_not_a[0];
string s0 = s.substr(i0);
auto z = z_function(s0);
// print s0 and z
dbg("s0: %s\n", s0.c_str());
dbg("z.size()=%d\n", z.size());
for (int i = 0; i < z.size(); i++) {
dbg("z[%d]=%d\n", i, z[i]);
}
ll ans = 0;
for (int len = 1; i0 + len <= n; len++) {
int to_left_a = i0;
int i = i0;
bool check = true;
while (true) {
if (to_not_a[i] == n) {
break;
}
if (z[i - i0] < len) {
check = false;
break;
}
assert(i + len <= n);
if (i + len == n) {
break;
}
int j = to_not_a[i + len];
if (j == n) {
break;
}
to_left_a = min(to_left_a, j - (i + len));
i = j;
}
if (check) {
ans += to_left_a + 1;
}
}
return ans;
}
void solve() {
for (const string &s : test_cases) {
printf("%lld\n", count_nonempty_strings(s));
}
}
int main() {
int t;
scanf("%d", &t); // Read number of test cases
test_cases.resize(t);
for (int i = 0; i < t; ++i) {
char buffer[MAXN];
scanf("%s", buffer);
test_cases[i] = string(buffer);
}
solve();
reset_globals();
return 0;
}