Good Triple

Good Triple

Created by LXC on Tue Mar 5 13:19:54 2024

https://codeforces.com/problemset/problem/1168/B

ranting: 1900

tag: brute force, two pointers

problem

给出01串s,求数对[l,r]个数,使得能找到至少一对[x,k],使1<=x,k<=|s|l<=x<x+2k<=rs[x]=s[x+k]=s[x+2k]

solution

对于长度为9的串必有数对[l,r]

我们考虑以i作为数对的第二个数共有多少数对,找到最大的k[i],使得s[ k[i], i ]中能找到至少一对合法数对。

i为右端点的贡献就是k[i]+1

code

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

#include <bits/stdc++.h>
#define SINGLE_INPUT
#define ll long long
#define ull unsigned long long
#define N 500005
#define MOD 998244353
#define INF 0x3f3f3f3f
using namespace std;

template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p) {
return os<<'['<<p.first<<", "<<p.second<<']';
}
template<class t> ostream& operator<<(ostream& os,const vector<t>& v) {
os<<'['; int s = 1;
for(auto e:v) { if (s) s = 0; else os << ", "; os << e; }
return os<<']';
}
template<class t,class u> ostream& operator<<(ostream& os,const map<t,u>& mp){
os<<'{'; int s = 1;
for(auto [x,y]:mp) { if (s) s = 0; else os << ", "; os<<x<<": "<<y; }
return os<<'}';
}

void sol() {
string s;
cin >> s;
ll n = s.size();
ll ans = 0;
vector<ll> k(n, -1);
for (ll i=1; i<n; i++) {
ll j;
k[i] = k[i-1];
for (j=1; i-2*j>k[i]; j++) {
if (s[i] == s[i-j] && s[i] == s[i-2*j]) {
k[i] = i-2*j;
break;
}
}
ans += k[i]+1;
}
cout << ans << "\n";
}

int main() {
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef SINGLE_INPUT
int t;
cin >> t;
while (t--) {
sol();
}
#else
sol();
#endif
return 0;
}