CF1168B Good Triple 题解
caohongxuan1 · · 题解
思路
易知,若一个三元组
因此,对于一个区间
我们可以固定区间左端点
于是,原题就能转化为:对于每一个
我们可以从右到左枚举区间左端点
首先考虑起点为
枚举
由于右端点
找到后,更新
而由于之前的
因此,左端点固定为
复杂度分析
时间复杂度为
空间复杂度为
注意,答案需要使用 long long 类型存储。
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
string s;
int n,ans;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>s;
s=' '+s;
n=s.size()-1;
int r=n+1;
for(int l=n;l>=1;l--){
for(int k=1;l+2*k<=n;k++){
if(s[l]==s[l+k]&&s[l+k]==s[l+2*k]){
r=min(r,l+2*k);
break;
}
}
ans+=max(n-r+1,0ll);
}
cout<<ans<<endl;
return 0;
}