标签:
题目大意:根据给出的数字串求较大的最小回文串
简单题,可以从中间开始比较,如果左边大于右边的话则终止,若右边大于左边的话则中间+1,然后向左边推。输出时按左边复制一遍。
#include<cstdio> #include<cstring> int t,i,j,l,r,n,pd,q,mid; char s[1000005]; int main(){ scanf("%d",&t); while(t--){ pd=q=l=r=0; scanf("%s",s); n=strlen(s); if(n%2==0)l=1; mid=n/2; while(l<=mid&&pd==0){ if (s[mid-l]==s[mid+r]){l++;r++;}else if (s[mid-l]<s[mid+r])break;else if (s[mid-l]>s[mid+r])pd=1; } if (pd==0){ l=1; if(n%2)l=0; s[mid-l]++; while(l<=mid&&s[mid-l]==58){ s[mid-l]=48; if(l<mid){ s[mid-l-1]++; } l++; } if (l>mid)q=1; } if (q){printf("1");for (i=1;i<n;i++) printf("0");printf("1\n"); } else { for(i=0;i<mid;i++) printf("%c",s[i]); for (n%2==0?i=mid-1:i=mid;i>=0;i--) printf("%c",s[i]); printf("\n"); } } }
SPOJ Problem 5:The Next Palindrome
标签:
原文地址:http://www.cnblogs.com/moris/p/4298980.html