Girls 'Research
Time Limit: 3000/1000 MS (Java/others) memory limit: 65535/32768 K (Java/Others)
Total submission (s): 537 accepted submission (s): 199
Problem descriptionone day, sailormoon girls are so delighted that they intend to research about palindromic strings. Operation contains two steps:
First step: Girls will write a long string (only contains lower case) on the paper. for example, "ABCDE", but 'A' inside is not the real 'A', that means if we define the 'B' is the real 'A ', then we can infer that 'C' is the real 'B', 'd is the real 'C '......, 'A' is the real 'Z'. According to this, string "ABCDE" changes to "bcdef ".
Second step: Girls will find out the longest palindromic string in the given string, the length of palindromic string must be equal or more than 2.
Inputinput contains multiple cases.
Each case contains two parts, a character and a string, they are separated by one space, the character representing the real 'A' is and the length of the string will not exceed 200000.all input must be lowercase.
If the length of string is Len, it is marked from 0 to len-1.
Outputplease execute the operation following the two steps.
If you find one, output the start position and end position of palindromic string in a line, next line output the real palindromic string, or output "no solution! ".
If there are several answers available, please choose the string which first appears.
Sample inputb babd A ABCD
Sample output0 2 azano solution!
Authorwangjing1111
Source 2010 "HDU-sailormoon" Programming Contest code: Question meaning: Given a character, use this character as the 'A' character, give a column: c abac, c =; B = z, A = y; then find out his longest echo string and mark his start position and final position... then output its return string (transformed) method: first use manacher to find its longest return string, algorithm start point, you can consider opening an array to store the original string .... then, output based on the start and end points, and use the manacher algorithm to process the response code:
1 # include <stdio. h> 2 # include <string. h> 3 # include <stdlib. h> 4 # define maxn 400050 5 char STR [maxn]; 6 int rad [maxn]; 7 int min (int A, int B) {8 return a <B? A: B; 9} 10 void Init (INT Len, char s []) {11 memset (rad, 0, sizeof (INT) * (2 * Len + 2 )); 12 s [Len * 2 + 2] = '\ 0'; 13 int I, j = 1; 14 for (I = Len * 2 + 1; I> 0; I --) {15 if (I & 1) s [I] = '#'; 16 else {s [I] = s [Len-J]; 17 J ++; 18} 19} 20 s [0] = '$'; // prevent overflow 21} 22 int manacher (INT Len) {23 int ID, I, ANS = 0; 24 For (I = 1; I <Len * 2 + 1; I ++) {25 if (ID + rad [ID]> I) rad [I] = min (RAD [ID * 2-I], ID + rad [ID]-I ); 26 while (STR [I-rad [I] = STR [I + rad [I]) rad [I] ++; 27 if (I + RA D [I]> ID + rad [ID]) id = I; 28 If (ANS <rad [I]) ans = rad [I]; 29} 30 return ans; 31} 32 int main () {33 char sav [2]; 34 int Len, I; 35 // system ("Call test. in "); 36 // freopen (" test. in "," r ", stdin); 37 // fclose (stdin); 38 While (scanf (" % S % s ", Sav, STR )! = EOF) {39 Len = strlen (STR); 40 Init (Len, STR); 41 int ans = manacher (LEN); 42 if (ANS <= 2) puts ("no solution! "); 43 else {44 for (I = 1; I <Len * 2 + 1; I ++) 45 if (ANS = rad [I]) break; 46 int St, en; 47 ST = (I-ans)/2; 48 en = ST + ans-2; 49 printf ("% d \ n", St, en); 50 for (Int J = (ST + 1) * 2; j <(ST + ans) * 2; j + = 2) {51 if (STR [J]-(SAV [0]-'A') <'A') 52 printf ("% C ", STR [J] + ('Z'-Sav [0] + 1); 53 else54 printf ("% C ", STR [J]-(SAV [0]-'A'); 55} 56 puts (""); 57} 58} 59 return 0; 60}
View code
HDU ---- (3294) Girls 'Research (manacher)