贪心,
如果字符串已经回文,在中间塞一个字符
abc->abbc
abba->abcba
如果不回文,从外向内找到第一个不回文字符
eg:
ad...la
显然只存在2种添法:ad...lda ald...la。一一验证即可。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
char s[100];
bool check(int i,int j)
{
while(i<=j)
{
if (s[i]!=s[j]) return 0;
i++,j--;
}
return 1;
}
int main()
{
// freopen("palindromes.in","r",stdin);
// freopen(".out","w",stdout);
scanf("%s",s+1);
int n=strlen(s+1);
For(i,n/2)
{
if (s[i]!=s[n-i+1])
{
if (check(i,n-i))
{
For(j,i-1) printf("%c",s[j]);
printf("%c",s[n-i+1]);
Fork(j,i,n) printf("%c",s[j]);
printf("\n");
return 0;
}
else if (check(i+1,n-i+1))
{
For(j,n-i+1) printf("%c",s[j]);
printf("%c",s[i]);
Fork(j,n-i+2,n) printf("%c",s[j]);
printf("\n");
return 0;
}
else break;
}
}
if (check(1,n))
{
int t=(1+n)/2;
For(j,t) printf("%c",s[j]);
printf("%c",s[t]);
Fork(j,t+1,n) printf("%c",s[j]);
printf("\n");
return 0;
}
printf("NA\n");
return 0;
}