題目描述
給定一個字元串,問是否能夠通過添加一個字母将其變成“回文串”。 “回文串”是指正着和反着讀都一樣的字元串。如:”aa”,”bob”,”testset”是回文串,”alice”,”time”都不是回文串。
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
bool check(char *str)//判斷這是不是一個回文字元串.
{
int i = 0;
int j = strlen(str) - 1;
while (i<j)
{
if (*(str + i) != *(str + j))
return false;
i++;
j--;
}
return true;
}
int main()
{
string s,s1,s2;
cin >> s;
s1 = s + s[0];
reverse(s.begin(), s.end());
s2 = s;
s2 = s2 + s2[0];
char *s3= (char*)s1.data();
char *s4 = (char*)s2.data();
if (check(s3) || check(s4))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
#include<iostream>
#include<string>
using namespace std;
bool check(string str)//判斷這是不是一個回文字元串.
{
int i = 0;
int j = str.size()-1;
while (i<j)
{
if (str[i] != str[j])
return false;
i++;
j--;
}
return true;
}
int main()
{
string s,s1,s2;
cin >> s;
s1 = s + s[0];
reverse(s.begin(), s.end());
s2 = s;
s2 = s2 + s2[0];
if (check(s1) || check(s2))
cout << "YES" << endl;
else
cout << "NO" << endl;
}