天天看点

给定一个字符串,问是否能够通过添加一个字母将其变成“回文串”

题目描述
									


给定一个字符串,问是否能够通过添加一个字母将其变成“回文串”。 “回文串”是指正着和反着读都一样的字符串。如:”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;
}