K:
Meaning of the questions: seeking palindrome string.
Ideas: Manacher algorithm template title
1 #include <stdio.h> 2 #include <algorithm> 3 #include <iostream> 4 #include <stdbool.h> 5 #include <stdlib.h> 6 #include <string> 7 #include <string.h> 8 #include <math.h> 9 #include <vector> 10 #include <queue> 11 #include <stack> 12 #include <set> 13 #include <map> 14 15 #define INF 0x3f3f3f3f 16 #define LL long long 17 #define MAXN 1000005 18 using namespace std; 19 20 char Ma[MAXN*2]; 21 int Mp[MAXN*2]; 22 char s[MAXN]; 23 24 void Manacher(char s[],int len) 25 { 26 int l = 0; 27 Ma[l++] = '$'; 28 Ma[l++] = '#'; 29 for (int i=0;i<len;i++) 30 { 31 Ma[l++] = s[i]; 32 Ma[l++] = '#'; 33 } 34 Ma[l++] = 0; 35 int mx = 0; 36 int id = 0; 37 for (int i=0;i<l;i++) 38 { 39 Mp[i] = mx>i?min(Mp[2*id-i],mx-i):1; 40 while (Ma[i+Mp[i]] == Ma[i-Mp[i]]) 41 Mp[i]++; 42 if (i+Mp[i]>mx) 43 { 44 mx = Mp[i]+i; 45 id = i; 46 } 47 } 48 } 49 50 int main() 51 { 52 ios_base::sync_with_stdio(0); 53 cin.tie(NULL); 54 //freopen("../in.txt","r",stdin); 55 int t = 1; 56 while (cin >> s) { 57 if (strcmp(s,"END") == 0) 58 break; 59 int len = strlen(s); 60 Manacher(s, len); 61 int ans = 0; 62 for (int i = 0; i < 2 * len + 2; i++) 63 ans = max(ans, Mp[i] - 1); 64 printf("Case %d: %d\n",t++,ans); 65 } 66 return 0; 67 }
L:
Idea: to determine whether the increase in Manacher algorithm palindrome length of time meets the left descending to ascending to the right of the multi-judge
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cmath> 6 using namespace std; 7 #define ll long long int 8 #define ull unsigned long long int 9 #define e 2.718281828459 10 #define INF 0x7fffffff 11 #pragma warning(disable:4996) 12 #define pf printf 13 #define sf scanf 14 #define max(a,b) (a)>(b)?(a):(b); 15 #define min(a,b) (a)<(b)?(a):(b); 16 #define pi acos(-1.0); 17 #define eps 1e-9; 18 #include <cstdlib> 19 using namespace std; 20 21 22 23 int n; 24 int ar[100005]; 25 int temp[200010]; 26 int p[200010]; 27 int manacher(int len) { 28 int i, j ; 29 int mid=0 ; 30 int ANS = 0 ; 31 is int MX = 0 ; 32 P [ 0 ] = . 1 ; 33 is for (I = . 1 ; <= len I; I ++ ) { 34 is J = 2 * MID - I; // J and i symmetrically about the mid 35 IF (i> = MX) P [i] = . 1 ; // beyond a, a character assignment. 1 36 the else P [i] = min (P [J], MX - i); / / both that a small, if mx - i> p [j] , i + palindromic described length does not exceed mx, mx - i <p [ j], then the i + p [i] exceeds the length of a palindromic mx palindrome can only take into mx-i and then manually match the 37 [ 38 is // whether or not exceeded, manual matching, I exceeds or does not exceed about both manually match 39 the while (TEMP [I - P [I]] == TEMP [I + P [I]] && TEMP [I + P [I] - 2 ]> = TEMP [I + P [I]]) // I - P [I]> =. 1 && I + P [I] <= len is omitted, since temp [0] and temp [len] different 40 P [I] ++ ; 41 is 42 is IF (I + P [I]> mx) { 43 is mx = I + P [I]; // update it as far as possible shifted mx 44 is MID = I; // update the midpoint MID 45 } 46 is ANS ANS => P [I] ANS:? P [I]; // record the maximum palindrome length 47 } 48 49 50 return ANS; 51 is } 52 is 53 is void the init ( int len) { 54 is TEMP [ 0 ] = - . 1 ; // first to prevent overflow -1 for 55 TEMP [ . 1 ] = - . 1 ; 56 is int I, J; 57 is 58 for (I = 2 , J = 0 ; J <len; J ++ ) { 59 TEMP [I ++] = Ar [J]; 60 TEMP [I ++] = - . 1 ; 61 is } 62 is TEMP [I] = - 2 ; //Last prevent overflow, and pay attention to a value different from the first, so that when a match is determined before omitted overflow 63 is } 64 65 int main ( void ) { 66 int T; 67 SF ( " % D " , & T ); 68 the while (T-- ) { 69 SF ( " % D " , & n-); 70 for ( int I = 0 ; I <n-; I ++) SF ( " % D " , & Ar [I]); 71 is the init (n-); 72 int the maxlen manacher = ( 2N-+ * . 1 ); // 2. 1 * + n-: most head and tail removed most 73 is PF ( " % D \ n- " , maxlen- . 1 ); 74 75 } 76 return 0 ; 77 }
M:
Meaning of the questions: input formats: character (ch) + space + string (s). S will first be converted. ch to be converted to 'a', the character should be a corresponding change in the other, such as ch = 'b' time. Then 'b' will convert 'a'. 'C' will convert 'b', 'd' to be converted to 'c', ...., 'a' to be converted to 'z'. Briefly, each letter s should be an offset occurs. S questions asked converted start and end positions of the longest substring of the palindrome, and outputs the substring, if there are a plurality of outputs. If not, the output '' No solution! ''
Ideas: First, to convert the string, and then after that Manacher algorithm. I think the difficulty of this problem is where the record about the location.
1 #include <stdio.h> 2 #include <algorithm> 3 #include <iostream> 4 #include <stdbool.h> 5 #include <stdlib.h> 6 #include <string> 7 #include <string.h> 8 #include <math.h> 9 #include <vector> 10 #include <queue> 11 #include <stack> 12 #include <set> 13 #include <map> 14 15 #define INF 0x3f3f3f3f 16 #define LL long long 17 #define MAXN 200005 18 using namespace std; 19 20 char Ma[MAXN*2]; 21 int Mp[MAXN*2]; 22 char s[MAXN]; 23 int cnt; 24 int middle; 25 26 void Manacher(char s[],int len) 27 { 28 cnt = 0; 29 int l = 0; 30 Ma[l++] = '$' ; 31 With the [++] = ' # ' ; 32 for ( you i = 0 ; i <len; i ++ ) 33 { 34 With the [++] = s [i]; 35 With the [++] = ' # ' ; 36 } 37 With the [++] = 0 ; 38 're mx = 0 ; 39 're a = 0 ; 40 for ( you i = 0 ; i <l; i ++ ) 41 { 42 Mp[i] = mx>i?min(Mp[2*id-i],mx-i):1; 43 while (Ma[i+Mp[i]] == Ma[i-Mp[i]]) 44 Mp[i]++; 45 if (i+Mp[i]>mx) 46 { 47 mx = Mp[i]+i; 48 id = i; 49 } 50 if (Mp[i]-1>cnt){ 51 cnt = Mp[i]-1; 52 middle = i; 53 } 54 } 55 } 56 57 int main() 58 { 59 ios_base::sync_with_stdio(0); 60 cin.tie(NULL); 61 char c; 62 while (cin >> c) 63 { 64 middle = 0; 65 cin >> s; 66 int len = strlen(s); 67 int dis = c-'a'; 68 for (int i=0;i<len;i++) 69 { 70 s[i] = (s[i]-'a'-dis+26)%26+'a'; 71 } 72 Manacher(s,len); 73 if (cnt == 1) 74 cout << "No solution!" << endl; 75 else 76 { 77 int l = middle-cnt+1; 78 int r = middle+cnt-1; 79 cout << l/2-1 << " " << r/2-1 << endl; 80 for (int i=l;i<=r;i++) 81 { 82 if (Ma[i] == '#' || Ma[i] == '$') 83 continue; 84 else 85 cout << Ma[i]; 86 } 87 cout << endl; 88 } 89 } 90 return 0; 91 }