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种状态,并且 ...
随机推荐
- [Angular 2] Controlling Rx Subscriptions with Async Pipe and BehaviorSubjects
Each time you use the Async Pipe, you create a new subscription to the stream in the template. This ...
- PHP函数spl_autoload_register()用法和__autoload()介绍(转)
详细出处参考:http://www.jb51.net/article/29624.htm 又是框架冲突导致__autoload()失效,用spl_autoload_register()重构一下,问题解 ...
- 对jquery的 attr()和prop()理解
jquery1.6版本后引入了prop()方法,很多时候总是混淆attr()与prop()这俩,下面分析一下这俩的区别 在此之前,先来了解一下html 的attribute和property,因为jq ...
- oc-15-self
// // Person.m // OC基础第三天 // // Created by apple on 15/10/17. // // #import "Person.h" @im ...
- WIN7 下 Qt Creator 安装 QWT
WIN7 下 Qt Creator 安装 QWT 环境:WIN7 +QT Creator2.6.2 1.下载QWT源代码 qwt-6.1-rc3.zip 2 编译QWT open projects- ...
- 基于jQuery实现苹果Dock样式的菜单
爱编程小编之前我们分享过相当数量的jQuery菜单了,今天要给大家带来一款Dock样式的jQuery菜单,用过苹果的朋友都知道,它的Dock菜单非常酷,配合漂亮的图标就更加绚丽了.效果图如下: 在线预 ...
- jodd-StringTemplateParser使用
StringTemplateParser 时一个string模板的解析器.在string模板中定义类似jsp标签的宏. 在解析过程中,宏被对值替换,值通过自定义的MacroResolver解析得到. ...
- Linux高级字符设备驱动
转载:http://www.linuxidc.com/Linux/2012-05/60469p4.htm 1.什么是Poll方法,功能是什么? 2.Select系统调用(功能) Select ...
- 解决python3 不能引入setuptools
1,原理分析: python中的setuptools因为其安全考虑需要ssl模块的支持.如果编译时没有通过ssl测试,就不能安装setuptools. 所以才会出现 Python3/dist-pack ...
- 使用idea创建maven的web项目
如果是第一次打开软件直接点击 Create New Project ,如果之前已经打开过项目了,需要点击菜单中 File → New Project … 如下图: 选择 Maven module ,输 ...