您的位置:首页 > 其它

Codeforces Round #286 (Div. 2) A. Mr. Kitayuta's Gift

2017-08-02 18:29 1001 查看
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.

Examples

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.

这道题当初比赛的时候没写出来,有思路但是卡在那里了。

这道题的思路就是先从外往里不断判断,遇到不同时错位一下,继续比较。

如果比较发现剩下的部分是一个回文串的话,那么在输出时加上缺少的字符即可。

还有记得判断当原本的字符串就是回文串的情况,这种情况下可以把中间的字符输出两次。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
char s[111];
bool check(int i,int j)
{
while(i<=j)
{
if(s[i]!=s[j])
return 0;
i++;j--;
}
return 1;
}
int main()
{
cin>>s;
int len=strlen(s);
for(int i=0;i<=len/2;i++)
{
if(s[i]!=s[len-1-i])
{
if(check(i+1,len-1-i))
{
for(int k=0;k<len-i;k++)
cout<<s[k];
cout<<s[i];
for(int k=len-i;k<len;k++)
cout<<s[k];
return 0;
}
else if(check(i,len-i-2))
{
for(int k=0;k<i;k++)
cout<<s[k];
cout<<s[len-1-i];
for(int k=i;k<len;k++)
cout<<s[k];
return 0;
}
else
break;
}
}
if(check(0,len-1))
{
for(int k=0;k<len/2;k++)
cout<<s[k];
cout<<s[len/2];
for(int k=len/2;k<len;k++)
cout<<s[k];
return 0;
}
cout<<"NA"<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: