About ABC468D
lg传送门
此题解中,设字符串长度为
首先分析如何判断一个字符串是不是好字符串。
对字符串
注意到对于
分别考虑长度为奇数和偶数的字符串:
- 对于长度为奇数的字符串,中心就是最中间的字符。如字符串
yandw,它的中心为n。 - 对于长度为偶数的字符串,中心取前一半的最后一个字符。如字符串
ylovew,它的中心为o。
此时考虑对于一个中心,如何扩展。分别枚举长度为奇数的字符串的中心和长度为偶数的字符串的中心。设双指针为
- 对于长度为奇数的字符串的中心,
l 、r 均初始化为该中心。此时中心可为1 到n 。 - 对于长度为偶数的字符串的中心,
l 初始化为该中心,r 初始化为该中心的下一个。由于r 的初始化,此时中心可为1 到n-1 。
此时,考虑扩展什么时候结束。如上述方案,记录在扩展时两指针对应的字符不同的次数,记为
AC Code:
#include <bits/stdc++.h>
#define el "\n"
#define sp " "
#define fi first
#define se second
#define inf 1e18
#define r0 return 0
#define int long long
#define F(i, a, b, c) for (int i = a; i <= b; i += c)
#define debug printf ("bug is in here\n")
#define TheEnd continue
#define base(s) s = sp + s
#define lcm(a, b) a / __gcd(a, b) * b
#define setp(x) fixed << setprecision(x)
#define getmid(l, r) l + (r - l) / 2
using namespace std;
typedef long long ll;
typedef string str;
using ull = unsigned long long;
str s;
int n;
//o&e mid
signed main(void) {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
cin >> s;
n = s.size();
base(s);
int ans = 0;
F(i, 1, n, 1) {//odd
int l = i, r = i;
int d = 0;
while (l > 0 && r <= n && l <= r) {
if (s[l] != s[r]) {
d++;
}
if (d > 1) {
break;
}
ans++;
l--, r++;
}
}
F(i, 1, n, 1) {//even
int l = i, r = i + 1;
int d = 0;
while (l > 0 && r <= n && l <= r) {
if (s[l] != s[r]) {
d++;
}
if (d > 1) {
break;
}
ans++;
l--, r++;
}
}
cout << ans << el;
r0;
}
//Yuexingfei_qwq is a flying-wind
赛时AC记录
求过,thk。