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.
The only line of the input contains a string s (1 ≤ |s| ≤ 10). Each character in s is a lowercase English letter.
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.
revive
reviver
ee
eye
kitayuta
NA
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.
传送门:http://codeforces.com/contest/505/problem/A
题意分析:1.字符串全为小写,长度不超过10 2.允许在字符串任意位置插入一个小写字母,判断是否能够变为回文串。 如果能, 输出该串。 不能, 输出 ‘NA’
MA:实在想不出什么好的优化算法, 只好暴力了~~ 思路是遍历每个位置,把字符串数组存到另一数组中,每次并空出一位, 枚举26个字母(其实可以只枚举原串中的就足够)直到变为回文串~~~ 真的是好粗暴。。
代码:
#include <cstring> #include <cstdio> const int maxn = 20 + 10; char s[maxn], b[maxn]; int is_p(int n) { for(int i = 0, j = n-1; i <= j; i++, j--) if(b[i] != b[j]) return 0; return 1; } int solve() { int n = strlen(s); memset(b, 0, sizeof(b)); for(int i = 0; i <= n; i++){ for(int j = 0; j < i; j++) b[j] = s[j]; for(int j = i; j < n; j++) b[j+1] = s[j]; for(char k = 'a'; k <= 'z'; k++){ //这里可以优化 b[i] = k; if(is_p(n+1)){ printf("%s ", b); return 1; } } } return 0; } int main() { while(~scanf("%s", s)){ if(!solve()) printf("NA "); memset(s, 0, sizeof(s)); } return 0; }