题解:B4101 [CSP-X2023 山东] 回文字符串
题目简述
如果能把一个字符串
给定字符串 YES,并输出 NO。
主要思路
如果两个字符串要满足相等或互为回文,那么长度一定相等;并且
所以可以遍历
但是存在一种情况,在
最后遍历完后,还可能会出现样例#3的情况,
AC Code
#include<map>
#include<set>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define endl '\n'
typedef long long ll;
typedef unsigned int ui;
typedef pair<int,int> pii;
typedef unsigned long long ull;
// ----------------------------
// ----------------------------
// ----------------------------
inline int read() {
int f=1,sum=0;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){sum=(sum<<1)+(sum<<3)+(ch^48);ch=getchar();}
return sum*f;
}
void print(int x) {
if(x<0){putchar('-');x=-x;}
if(x>9){print(x/10);}
putchar(char(x%10+'0'));
}
bool check(string s1,string s2) {
if (s1 == s2) return true;
reverse(s2.begin(),s2.end());
return s1 == s2;
}
int main() {
string s;
cin>>s;
// ------------------------
int ans = 0;
string s1 = "";
string s2 = "";
int n = s.length();
for (int i=0;i<(n+1)/2;i++) {
s1 += s[i];
s2 = s[n-i-1] + s2;
if (check(s1,s2)) {
s1 = "";
s2 = "";
ans += 2;
if (n&1 && i==n/2) ans--;
}
}
if (ans && s1!="" && s2!="") ans++;
// ------------------------
if (!ans) cout<<"NO";
else {
cout<<"YES"<<endl;
print(ans);
}
return 0;
}