题解:P12143 [蓝桥杯 2025 省 A] 好串的数目
P12143 [蓝桥杯 2025 省 A] 好串的数目
思路
滑动窗口
先来分析一些性质。阅读题目,我们发现,连续非递减子串必须满足:对于相邻字符
我们可以枚举子串的右端点下标
Code
可以用变量
#include <bits/stdc++.h>
using namespace std;
const int nn = 100005;
char s[nn];
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cin >> s;
int brk = 0, l = 0;
int64_t ans = 0;
for (int r = 0; s[r]; r++) {
if (r && s[r - 1] + 1 != s[r] && s[r - 1] != s[r])
brk++;
while (brk > 1) {
l++;
if ((s[l] != s[l - 1] && s[l - 1] + 1 != s[l]))
brk--;
}
ans += r - l + 1;
}
cout << ans << '\n';
return 0;
}
时间复杂度
时间复杂度为