您的位置:首页 > 其它

CF 505A(Mr. Kitayuta's Gift-回文串)

2015-01-19 14:46 405 查看
A. Mr. Kitayuta's Gift

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Mr. Kitayuta has kindly given you a string s consisting of lowercase English letters. You are asked to insert exactly one lowercase English letter into s to
make it a palindrome. A palindrome is a string that reads the same forward and backward. For example, "noon",
"testset" and "a" are all palindromes, while "test"
and "kitayuta" are not.

You can choose any lowercase English letter, and insert it to any position of s, possibly to the beginning or the end of s.
You have to insert a letter even if the given string is already a palindrome.

If it is possible to insert one lowercase English letter into s so that the resulting string will be a palindrome, print the string after the insertion.
Otherwise, print "NA" (without quotes, case-sensitive). In case there is more than one palindrome that can be obtained, you are allowed to print any of them.

Input

The only line of the input contains a string s (1 ≤ |s| ≤ 10).
Each character in s is a lowercase English letter.

Output

If it is possible to turn s into a palindrome by inserting one lowercase English letter, print the resulting string in a single line. Otherwise, print "NA"
(without quotes, case-sensitive). In case there is more than one solution, any of them will be accepted.

Sample test(s)

input
revive


output
reviver


input
ee


output
eye


input
kitayuta


output
NA


Note

For the first sample, insert 'r' to the end of "revive" to
obtain a palindrome "reviver".

For the second sample, there is more than one solution. For example, "eve" will also be accepted.

For the third sample, it is not possible to turn "kitayuta" into a palindrome by just inserting one letter.

给一个小写字母字符串,必须添一个字符,使添后的字符串回文,求添后的字符串。

贪心,

如果字符串已经回文,在中间塞一个字符

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: