codeforces Round 286# problem A. Mr. Kitayuta's Gift
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 = + ;
char s[maxn], b[maxn];
int is_p(int n)
{
for(int i = , j = n-; i <= j; i++, j--) if(b[i] != b[j]) return ;
return ;
} int solve()
{
int n = strlen(s);
memset(b, , sizeof(b));
for(int i = ; i <= n; i++){
for(int j = ; j < i; j++) b[j] = s[j];
for(int j = i; j < n; j++) b[j+] = s[j];
for(char k = 'a'; k <= 'z'; k++){ //这里可以优化
b[i] = k;
if(is_p(n+)){
printf("%s\n", b);
return ;
}
}
}
return ;
}
int main()
{
while(~scanf("%s", s)){
if(!solve()) printf("NA\n");
memset(s, , sizeof(s));
}
return ;
}
codeforces Round 286# problem A. Mr. Kitayuta's Gift的更多相关文章
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift
题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...
- CodeForces Round #286 Div.2
A. Mr. Kitayuta's Gift (枚举) 题意: 给一个长度不超过10的串,问能否通过插入一个字符使得新串成为回文串. 分析: 因为所给的串很多,所以可以枚举 “在哪插入” 和 “插入什 ...
- Codeforces 505A Mr. Kitayuta's Gift 暴力
A. Mr. Kitayuta's Gift time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 【CF506E】Mr. Kitayuta's Gift dp转有限状态自动机+矩阵乘法
[CF506E]Mr. Kitayuta's Gift 题意:给你一个字符串s,你需要在s中插入n个字符(小写字母),每个字符可以被插在任意位置.问可以得到多少种本质不同的字符串,使得这个串是回文的. ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集
D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs
B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph
D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...
- Codeforces Round #286 (Div. 1) 解题报告
A.Mr. Kitayuta, the Treasure Hunter 很显然的一个DP,30000的数据导致使用map+set会超时.题解给了一个非常实用的做法,由于每个点有不超过250种状态,并且 ...
随机推荐
- [Ember] Ember.js Templates
In this lesson, we'll go over some of the basics of Ember.js templates and how they work with contro ...
- poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)
http://poj.org/problem?id=1222 题意:给一个确定的5*6放入矩阵.每一个格子都有一个开关和一盏灯,0表示灯没亮,1表示灯亮着.让你输出一个5*6的矩阵ans[i][j], ...
- 分享一款页面视差滚动切换jquery.localscroll插件
今天给大家分享一款页面视差滚动切换jquery.localscroll插件. 滚动鼠标液动条看下页面的切换效果.该插件适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera ...
- html元素英文含义
常用html标签的英语全称及简单功能描述 <a>:anchor 定义锚 <abbr>:abbreviation 定义缩写 <acronym>: 定义只取消首字母的缩 ...
- C#中常用的排序算法的时间复杂度和空间复杂度
常用的排序算法的时间复杂度和空间复杂度 常用的排序算法的时间复杂度和空间复杂度 排序法 最差时间分析 平均时间复杂度 稳定度 空间复杂度 冒泡排序 O(n2) O(n2) 稳定 O(1) 快速排序 ...
- 重构4-Push Down Method(方法下移)
我们介绍了将方法迁移到基类以供多个子类使用的上移方法重构,今天我们来看看相反的操作.重构前的代码如下: public abstract class Animal { public void Bark( ...
- Oracle基础 PL-SQL编程基础(4) 异常处理
异常处理: 即使良好的PL-SQL程序也会遇到错误或者未预料的事件,一个优秀的程序都应该能够处理各种出错情况,尽可能的从错误中恢复.程序在运行时出现的错误成为异常.发生异常后,语句讲终止执行,PLSQ ...
- linux_iptables 详解
iptables工具__过滤包—命令(-A.-I.-D.-R.-L等).参数(-p.-s.-d.--sport.--dport.-i.-o等).动作-j (ACCEPT.DROP.REJECT.RED ...
- ubuntu创建、删除文件及文件夹,强制清空回收站方法
mkdir 目录名 => 创建一个目录 rmdir 空目录名 => 删除一个空目录 rm 文件名 文件名 => 删除一个文件或多个文件 rm –rf 非 ...
- XE5 Android 开发数据访问手机端 解决乱码的办法
经过测试,将sqlserver里的字段由varchar 或者char 改为 nvarchar 或者nchar 然后在手机端的clientdataset 增加字段的时候数据类型选择widestrin ...