505A - Mr. Kitayuta's Gift 字符串 枚举 蛮力

来源:互联网 发布:开源java订单管理系统 编辑:程序博客网 时间:2024/06/05 07:54

1.题目:


A. Mr. Kitayuta's Gift
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 intos to make it a palindrome. Apalindrome 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 ins 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.
Sample test(s)
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.



2.代码:

把长度最多为10的字符串从长度为0开始枚举,枚举的位置插入数字‘0’(注意插入函数只能插入字符串)。然后换成对称位置的字母。判断是否形成回文字符串。循环结束返回“NA”。

#include <iostream>#include<cmath>#include<cstdio>#include<cstdlib>#include<algorithm>#include<ctime>#include<cstring>#include<queue>#include<stack>#include<string>using namespace std;bool check(const string &s){for(int i=0;i<s.size();i++)if(s[i]!=s[s.size()-1-i])return false;return true;}int main(){    string s;    cin>>s;    for(int  i=0;i<=s.size();i++)    {        string s2=s;        s2.insert(i,"0");        s2[i]=s2[s2.size()-1-i];        if(check(s2))        {            cout<<s2;            return 0;        }    }   cout<<"NA";    return 0;}</span>


3.知识点:


string知识点

用#include <string>注意不是#include <string.h>或者其他

using namespace std;


string s;

s.insert(i,"0");

string类插入函数

string.insert(int pos,const char *s,int n)在string的pos位置插入s的前n个字符。


string类的其他函数

赋值函数:string.assign(const char *s,int n)将s的前n个字符赋值给string。

连接函数:string.append(const char *s,int n)将s的前n个字符连接到string的字符串尾。

比较函数:string,conpare(int pos,int n,const string &s2,int pos2,int n2)string的pos位置开始n个字符与s2的pos2开始                       n2个字符比较大小,大返回1,相等返回0,小返回-1。

取子串:string.substr(int pos,int n)取string的POS开始的前n个字符作为字符串

交换函数:string.swap(string& s2);交换两字符串。

查找函数:string.find(char c,int pos);pos位置开始c字符的位置。

  string.find(const char *s,int pos,int n);pos开始的string字符串中s字符数组所前n个字符在的位置。

  string.find(const string &s,int pos);从pos开始查找字符串s所在的位置。

替换函数:string.replace(int pos,int n,const char *s,int n2);删除pos位置开始的n个字符并且插入s的前n2个字符。







0 0
原创粉丝点击