https://ac.nowcoder.com/acm/contest/330/I
C++版本一
题解:
std
可以认为插入和删除是等价的操作。想到这一点,这题就会好做很多。
如果这个串本身就是回文串,答案一定是Yes。
否则我们只需要考虑串中对称的位置不相等的两个字符,分别尝试把它们删掉后判断一下是不是回文的就行了。
#include <bits/stdc++.h>
using namespace std;
int check(const string& s)
{
int n = s.length();
for (int i = 0; i < n; i++)
if (s[i] != s[n - 1 - i])
return i;
return -1;
}
int main()
{
string s;
cin >> s;
int diff = check(s);
if (diff == -1)
cout << "Yes" << endl;
else
{
string tmp = s;
string s1 = s.erase(diff, 1);
string s2 = tmp.erase(tmp.length() - 1 - diff, 1);
if (check(s1) == -1 || check(s2) == -1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
C++版本二
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q;
int ans,cnt,flag,temp;
int a[N];
char str[N],tmp[N];
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
scanf("%s",str);
//scanf("%d",&t);
//while(t--){}
int len=strlen(str);
flag=0;
for(int i=0;i<len;i++){
if(str[i]!=str[len-i-1]){
flag=1;
cnt=i;
}
}
if(flag){
int l=0;
for(int i=0;i<len;i++){
if(i!=cnt){
tmp[l++]=str[i];
}
}
ans=0;
for(int i=0;i<len-1;i++){
if(tmp[i]!=tmp[len-i-2]){
ans=1;
}
}
if(ans){
l=0;
for(int i=0;i<len;i++){
if(len-i-1!=cnt){
tmp[l++]=str[i];
}
}
ans=0;
for(int i=0;i<len-1;i++){
if(tmp[i]!=tmp[len-i-2]){
ans=1;
}
}
}
if(ans){
cout<<"No"<<endl;
}else{
cout<<"Yes"<<endl;
}
}else{
cout<<"Yes"<<endl;
}
//cout << "Hello world!" << endl;
return 0;
}