题目描述
给定一个字符串,问是否能通过添加一个字母将其变为回文串。
输入描述:
一行一个由小写字母构成的字符串,字符串长度小于等于10。
输出描述:
输出答案(YES\NO).
示例1
输入
coco
输出
YES
#include <iostream>
#include <iomanip>
#include<string>
#include<algorithm>
#include <math.h>
#include<limits.h>
using namespace std;
bool huiwen(const string& s){
string r = s;
reverse(r.begin(), r.end());
return r == s;
}
int main(){
string str;
bool flag = false;
while(cin >> str){
if(str.size() <= 1)
{
cout << "YES" << endl;
continue;
}
else
{
int len = str.size();
bool flag = false;
for(int i = 0; i < len; ++i)
{
string strTemp = str;
strTemp.erase(strTemp.begin() + i);
//或者 strTemp.erase(i,1);
if(huiwen(strTemp))
{
flag = true;
cout << "YES" << endl;
break;
}
}
if(!flag)
{
cout << "NO" << endl;
}
}
}
return 0;
}