题解:AT_abc449_c [ABC449C] Comfortable Distance
Bing_laoqi114 · · 题解
AT_abc449_c题解
蒟蒻的第一篇题解,求过
题目简述
有一个长度为
-
1 \leq i \leq j \leq N -
S_i = S_j -
L \leq j - i \leq R
思路
1.暴力枚举每个
注意到字符串仅由26种字符构成,可以开一个数组[27][500005]单独存每一种字符的出现地点。肯定不会MLE因为我过了
3.然后对于每种字符(以 a 为例),尝试以每个第
4.对于这个起点
5.最终输出答案。
时间
AC CODE
#include<bits/stdc++.h>
#define I return
#define AK 0
#define IOI ;
using namespace std;
int n, m, k, q, a[30][500005], l, r;
long long ans;
string s;
int main() {
cin >> n >> l >> r >> s;
for (int i = 0; i < n; i++)a[s[i] - 'a' + 1][++b[s[i] - 'a' + 1]] = i;
for (int i = 1; i <= 26; i++) {
if (b[i] == 0) continue;
int y1 = 1,y2 = 1;
for (int x = 1; x <= b[i]; x++) {
while (y1 <= b[i] && (a[i][y1] - a[i][x] < l || y1 <= x)) y1++;
if (y2 < y1) y2 = y1;
while (y2 <= b[i] && a[i][y2] - a[i][x] <= r) y2++;
if (y2 > y1) ans += y2 - y1;
}
}
cout << ans << endl;
I AK IOI//I AK IOI!
}