codeforces 1168 B. Good Triple(思维+暴力)

时间:2021-08-23
本文章向大家介绍codeforces 1168 B. Good Triple(思维+暴力),主要包括codeforces 1168 B. Good Triple(思维+暴力)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

传送门

题意:

给出一个长度为\(n\)\(01\)串,若一个区间满足以下条件,则是合法的,

存在整数\(x,k\) 满足 \(l\leq x <x+2k \leq r\)    \(s_x=s_{x+k}=s_{x+2k}\)

求有多少区间是合法的

题解:

看完这道题感觉一点思路都没有,于是去看了题解,大受震撼

事实上,一个长度为\(9\)的区间内必定存在一个满足条件的位置,因此直接暴力找即可。

至于为什么是长度为\(9\)之内必定有,可能是因为\(01\)串的关系,所有使得上述情况很容易出现。

由这题可以告诉我们,有些没有思路的题,可以尝试先暴力试下,说不定就是正解。

代码:

#pragma GCC diagnostic error "-std=c++11"
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<ctime>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
const int mod=1e9+7;
const int MAXN=5e5+5;
const int inf=0x3f3f3f3f;
char s[MAXN];
int main()
{
    cin >> s + 1;
    int n = strlen(s + 1);
    int r = n + 1;
    ll ans = 0;
    for (int i = n - 2; i >= 1;i--)
    {
        for (int j = 1; i + 2 * j <= n;j++)
        {
            if(s[i]==s[i+j]&&s[i]==s[i+2*j]){
                r = min(r, i+2*j);
                break;
            }
        }
        
        if(r<=n){
            ans = ans + (n - r + 1);
        }
    }
    cout << ans << endl;
}

原文地址:https://www.cnblogs.com/TheBestQAQ/p/15177499.html