codeforces1169D

给你一个由01组成的序列,问你有多少对x,y使得[x,y]里面可以找到三位置他们等差且值相同。

例如:010101中1,5;2,6;1,6;有三对。

解法:构建一个等差子序列的左侧temp。遍历整个序列。对每一个位置遍历等差子序列的差值,找到它前面是否能构成等差子序列。如果找到的那个位置小于temp或者找不到时,答案直接加上temp(作为前面已经有的等差子序列的右侧y)。否则更新temp并加上贡献。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<queue>
#include<vector>
#include<string>
#define ll long long
#define PI acos(-1.0)
#define F first
#define S second
#define pb push_back
#define debug(x); printf("debug%d
",x);
#define des(x); printf("des:%s
",x+1);
const ll INF=0x3f3f3f3f3f3f3f3f;
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
using namespace std;
const int N=3e5+5;
char s[N];
int main()
{
    scanf("%s",s+1);
    int len=strlen(s+1);
    ll ans=0;
    int temp=0;
    for(int i=1;i<=len;i++)
    {
        for(int j=1;i-2*j>=temp;j++)
        {
            if(s[i]==s[i-j]&&s[i]==s[i-2*j])
            {
                temp=i-2*j;
                break;
            }
        }
        ans+=(ll)(temp);
    }
    printf("%lld
",ans);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/switch-waht/p/13512719.html