URAL 1354. Palindrome. Again Palindrome

来源:互联网 发布:淘宝产品上首页 编辑:程序博客网 时间:2024/04/29 08:24

题意:给定一字符串,在其后加一个最短的非空字符串使得整个字符串是回文串。

中间偏右一点开始,枚举对称中心,一旦符合条件就输出。

对于字符串 abcd   0表示对称中心为a,1表示对称中心在a,b之间,2表示对称中心为b,依此类推。

这样就省去了奇偶性的讨论。

int main(void){string str;while(cin>>str){int n=str.length();if(n==1) {printf("%c%c\n",str[0],str[0]);continue;}int N=(n-1) <<1;for(int i=n;i<=N;i++){int l=i/2,r=(i+1)/2;while(str[l]==str[r]&&r<n) l--,r++;if(r==n){//匹配成功 for(int I=0;I<=i/2;I++){printf("%c",str[I]);}for(int I=(i-1)/2;I>=0;I--){printf("%c",str[I]);}cout<<endl;break;}}}return 0;}





0 0