Warm up 3 [C] Casino
你为何这么叼!
[C] Casino
Dalia is the assistant director of the fundraising team in the ACPC. She is facing a difficult time this year, there’s a huge lack of sponsors! And now we are facing the danger of not being able to provide the teams with balloons, T-shirts or even name-tags.
Dalia knows it is too late to get a sponsor, actually too late to do anything. But she doesn’t simply give up; she decided that her only hope is to gamble. She will go to a casino where they just invented a new game; she thinks she might have a more promising chance if she plays that game.
The game is very simple, the dealer puts a long string of cards on the table in front of Dalia and she is required to point out palindromes longer than one character (words that are read backward the same as forward) of maximum length (a maximum length palindrome is a palindrome that no other palindrome exists in the string with greater length). So if the maximum length of palindrome in a string is X>1, print all palindromes of length X in the string.
Input Specification
Input will start with T number of test cases. Each test case will consist of 1 line that contains a non- empty string S of lower case English letters no longer than 1000 characters.
Output Specification
For each test case, print a line containing the case number as shown in the sample then print the palindromes each on a line by itself, in the order of their occurrence in S from right to left.
Sample Input
2 abcba abba
Sample Output
Case #1:
abcba
Case #2:
abba
1 #include <cstdio> 2 #include <vector> 3 #include <cstring> 4 #include <queue> 5 #include <algorithm> 6 using namespace std; 7 #define maxn 1005 8 #define mod 1000000007 9 #define ll long long 10 int n,m,k; 11 int a[maxn],b[maxn]; 12 char s[maxn]; 13 int main(){ 14 int t,cas=1; 15 scanf("%d",&t); 16 while(t--){ 17 memset(a,0,sizeof a); 18 memset(b,0,sizeof b); 19 printf("Case #%d:\n",cas++); 20 scanf("%s",s); 21 n=strlen(s); 22 for(int i=0;i<n-1;i++) 23 for(int j=1;i+j<n&&i+1-j>=0;j++) 24 if(s[i+j]==s[i+1-j])a[i]++;else break; 25 for(int i=0;i<n-1;i++) 26 for(int j=1;i+j<n&&i-j>=0;j++) 27 if(s[i+j]==s[i-j])b[i]++;else break; 28 int k=0; 29 for(int i=0;i<n;i++)k=max(k,max(a[i]*2,b[i]*2+1)); 30 for(int i=n-1;i>=0;i--){ 31 if(k%2==1&&k>=3)if(b[i]==(k-1)/2){for(int j=i-(k-1)/2;j<=i+(k-1)/2;j++)printf("%c",s[j]);printf("\n");} 32 if(k%2==0&&k>=2)if(a[i]==k/2){for(int j=i-k/2+1;j<=i+k/2;j++)printf("%c",s[j]);printf("\n");} 33 } 34 } 35 return 0; 36 }
浙公网安备 33010602011771号