https://codeforces.com/gym/102428/problem/E
题意
给出仅由’P’和’E’组成的字符串(首尾相连),问长度不超过s且至少存在一个’E’的子串有多少个。
思路
因为只有两种情况:包含’E’或不包含’E’。可以先求出全部的情况再减去只含’P’的情况;然后看了题解,大佬们用的尺取法,绝了,原来还可以这样写。学到了,循环串二倍处理_(:з」∠)_
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
char a[maxn * 2];
int p[maxn], idx;
int main()
{
int m, n;
cin >> a + 1 >> m;
n = strlen(a + 1);
for(int i = 1;i <= n; i++)
{
a[i + n] = a[i];
}
for(int i = 0;i <= 2 * n; i++)
{
if(a[i] == 'E') p[++idx] = i;
}
if(idx == 0) cout << 0 << endl;
else
{
ll ans = 0;
int last = 1;
for(int i = 1;i <= n; i++)
{
while(i > p[last] && last <= idx) last++;
if(last > idx) break;
ans += max(0, m - (p[last] - i));
}
cout << ans << endl;
}
return 0;
}