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

来源:互联网 发布:免费基金模拟软件 编辑:程序博客网 时间:2024/06/05 09:11

原题链接
A. Mr. Kitayuta’s Gift
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
题意:求插入一个字符使其为回文串。由于这个题数据非常小,所以可以直接暴力。但是当数据大了以后就需要从中间二分这个字符串,如果从中间往两边扫的过程中有两个字符不一样那么就补上这个字符,然后再往后扫。如果之后两边都一样了那么就说明没有问题了,如果还有不一样的就说明不能只插入一个字符构成回文串

//http://codeforces.com/problemset/problem/505/A#include <algorithm>#include <iostream>#include <utility>#include <sstream>#include <cstring>#include <cstdio>#include <vector>#include <queue>#include <stack>#include <cmath>#include <map>#include <set>using namespace std;typedef long long ll;const int MOD = int(1e9) + 7;//int MOD = 99990001;const int INF = 0x3f3f3f3f;const ll INFF = (~(0ULL)>>1);const double EPS = 1e-9;const double OO = 1e20;const double PI = acos(-1.0); //M_PI;const int fx[] = {0,-1,1};const int maxn=1000 + 5;char a[maxn];char b[maxn];void str(int loc,char c,int len){    for(int i=0;i<loc;i++)        b[i]=a[i];    b[loc]=c;    for(int i=loc+1;i<=len+1;i++)        b[i]=a[i-1];}bool solve(){    int len=strlen(b);    int mid=len/2;    for(int i=0;i<mid;i++){        if(b[i]!=b[len-i-1]){            return false;        }    }    return true;}int main(){    while(scanf("%s",a)==1){        int len=strlen(a);        for(int i=0;i<=len;i++){            for(char c='a';c<='z';c++){                str(i,c,len);//构建字符串                if(solve()){//判断字符串                    cout << b << endl;                    goto END;                }            }        }        printf("NA\n");        END:;    }    return 0;}
0 0
原创粉丝点击