最近做的几条题(3)Palindrome

 Decription:

"Palindrome" means a word or phrase such as "deed" or "level", which is the same when you read it backwards. Our task here is not to identify palindromes, because it is too easy a job.

 

Considering a common word, we can add some letters to its end to make it become a "palindrome-like" string. For example, we can append letter "a" and "c" to the word "cat" and make up a "palindrome-like" string "catac". We can also add more letters to make it become "catitac", but it is longer than "catac".

 

Your task is to output a shortest "palindrome-like" string base on a given word. Be aware of that you are only allowed to add letters to the end of the given word.

Input

The input consists of some test cases. Each test case starts with a line containing a positive integer n (n <= 10) indicating the number of test cases. This is followed by n lines and each line contains a word which consists of only lowercase letters.

Output

For each case, print the shortest palindrome-like string in a single line without any white spaces or other unnecessary characters.

Sample Input

3

cat

deed

here

Sample Output

catac

deed

hereh

程序:

#include<iostream>
#include<string>
#include<vector>
using namespace std;

void Palindrome(string &S_str)
{
 string C_str;
 int Sp = 0;
 int Cp = 0;
 for(int i = S_str.size()-1;i>=0;i--)  //倒叙
 {          
  C_str += S_str[i];
 }
 
 for(Sp = 0;Sp<S_str.size();Sp++)
 {
  if(S_str[Sp] == C_str[Cp])
  {
   Cp++;
  }
  else
  {
   if(Cp>0)
    Cp--;
  }
 }
 if(Cp != C_str.size())
 S_str += C_str.substr(Cp,C_str.size()-1);
}

void main()
{
 int lines;
 string test;
 cin>>lines;
 vector<string>Answer;
 for(int i = 0; i<lines;i++)
 {
  cin>>test;
  Palindrome(test);
  Answer.push_back(test);
 }
 for(i = 0;i<Answer.size();i++)
 {
  cout<<Answer[i]<<endl;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值