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种状态,并且 ...
随机推荐
- MySQL Spatial Extensions 地理信息
http://dev.mysql.com/doc/refman/5.7/en/gis-data-formats.html http://mysqlserverteam.com/mysql-5-7-an ...
- MySQL优化---DBA对MySQL优化的一些总结
非原创, 来自梦嘉朋友, 非常好的总结, 一起学习. ------------------------------------------------- 1. 要确保有足够的内存数据库能够高效的运 ...
- HTML目录
1. 表格标记 2. HTML常用标记 3. 博客园添加访问人数统计 4. 如何快速掌握CSS(各种CSS工具) 5. HTTP协议状态码详解(HTTP Status Code)(转)
- Qt界面美化 QSS
目前发现在Qt-Design中右击控件,可以选择Change StyleSheet ------------------------以下总结不太对 刚接触Qt,发现Qt Design无法对每个控件进行 ...
- 十六进制字节 & 十六进制转二进制
做项目也将近一年的时间了.从一开始就经常提到“一个十六进制字节”,然而一开始就是迷惑的,直到现在. 一个十六进制字节,比如:FF.周围的人经常说这是一个十六进制字节.然后我就想,这不是两个字符 ...
- windows10上安装 .NET Framework 3.5
在安装一些软件时,需要 .NET Framework3.5.按照windows给的提示下载不了.在官方网站上给了解决方案: 运行 DISM 工具 从屏幕右边缘向中间轻扫,然后点击“搜索”.(如果使用的 ...
- C++ atol
函数名: atol 功 能: 把字符串转换成长整型数 用 法: long atol(const char *nptr); 简介编辑 相关函数: atof,atoi,strtod,strtol,st ...
- JavaScript组成
JavaScript由ECMAScript,Dom,Bom三部分组成. ECMAScript是一种由Ecma国际(前身为欧洲计算机制造商协会,英文名称是European Computer Manufa ...
- 沈逸老师PHP魔鬼特训笔记(8)
创建模板文件: 这节课老师带领我们开始创建TEMPLATE,按照老师教导的思路,我们希望这样一种代码写法:1.譬如我定义一个变量$name=‘’;2.然后呢我读取一个模板.3.再然后我在这个模板里 设 ...
- 关于java设计模式与极品飞车游戏的思考
------- android培训.java培训.期待与您交流! ---------- 对像我一样正在学习java的人来讲,对设计模式的学习是个很重要的环节.而我们在学习设计模式时,不仅仅应该知道它们 ...