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

原题链接
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

门豪杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值