Codeforces Round #286 (Div. 2) A. Mr. Kitayuta's Gift
来源:互联网 发布:重庆时时彩 遗漏数据 编辑:程序博客网 时间:2024/06/04 19:26
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.
这道题当初比赛的时候没写出来,有思路但是卡在那里了。
这道题的思路就是先从外往里不断判断,遇到不同时错位一下,继续比较。
如果比较发现剩下的部分是一个回文串的话,那么在输出时加上缺少的字符即可。
还有记得判断当原本的字符串就是回文串的情况,这种情况下可以把中间的字符输出两次。
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<string>using namespace std;char s[111];bool check(int i,int j){while(i<=j){if(s[i]!=s[j])return 0;i++;j--;}return 1;}int main(){cin>>s;int len=strlen(s);for(int i=0;i<=len/2;i++){if(s[i]!=s[len-1-i]){if(check(i+1,len-1-i)){for(int k=0;k<len-i;k++)cout<<s[k];cout<<s[i];for(int k=len-i;k<len;k++)cout<<s[k];return 0;}else if(check(i,len-i-2)){for(int k=0;k<i;k++)cout<<s[k];cout<<s[len-1-i];for(int k=i;k<len;k++)cout<<s[k];return 0;}elsebreak;}}if(check(0,len-1)){for(int k=0;k<len/2;k++)cout<<s[k];cout<<s[len/2];for(int k=len/2;k<len;k++)cout<<s[k];return 0;}cout<<"NA"<<endl;return 0;}
- Codeforces Round #286 (Div. 2)-A. Mr. Kitayuta's Gift
- Codeforces Round #286 (Div. 2) A. Mr. Kitayuta's Gift
- Codeforces Round #286 (Div. 2) A - Mr. Kitayuta's Gift ( 暴力枚举)std:string::insert
- 暴力字符串 Codeforces Div. 2 A Mr. Kitayuta's Gift
- [Codeforces Round #286 DIV1E (CF506E)] Mr. Kitayuta's Gift
- Codeforces Round #286 (Div. 2) D.Mr. Kitayuta's Technology
- Codeforces 505A Mr. Kitayuta's Gift
- codeforces Round 286# problem A. Mr. Kitayuta's Gift < 回文串 >
- A. Mr. Kitayuta's Gift
- A. Mr. Kitayuta's Gift
- A. Mr. Kitayuta's Gift
- Codeforces 505A. Mr. Kitayuta's Gift 水
- Codeforces 505A Mr. Kitayuta's Gift 题解
- Codeforces Round #286 (Div. 2)B. Mr. Kitayuta's Colorful Graph
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph +foyd算法的应用
- B. Mr. Kitayuta's Colorful Graph( Codeforces Round #286 (Div. 2))
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs
- 划分树
- POJ 3037 Skiing (搜索 或 spfa)
- android性能优化之布局优化
- Android动画学习(五)之属性动画实现Tween的效果和高级属性示例
- c#微信服务器配置
- Codeforces Round #286 (Div. 2) A. Mr. Kitayuta's Gift
- 可变参数与自动拆箱和自动装箱
- 计算几何小结:叉积
- 实验:minikuber上运行简单程序
- Woocommerce Product Filter插件——为不同类别设置不同filter
- 生活中的单例——只爱一个人
- Tomcat单机多实例部署实战
- spring JdbcTemplate经典案例
- rom eeprom ram flash 的区别