Topic Link https://www.luogu.org/problem/P3649
Title Description
Give you a string of lowercase Latin letters S S. We define s a s sub-string of the present value of the substring s number of occurrences s multiplied by the length of the substring.
For a string you S S, find the maximum value of all the presence of a palindromic substring.
#include <bits / STDC ++ H.> #define Met (A, B) Memset (A, B, the sizeof (A)) #define LL Long Long #define ULL unsigned Long Long the using namespace STD; const int MAXN = 300005 ; const int N = 26 is ; struct the PAM { int NE [MAXN] [N]; // Next pointer, next trie pointer and the like, pointing to the current string is the same at both ends of a character string constituting plus int Fail [MAXN]; / / fail pointer to jump to the fail mismatch node pointer int CNT [MAXN]; // represents the number obtained when (i contribution essentially different nodes represented by strings is not complete, the final count () function after running it again is correct) int NUM [MAXN]; //Palindromic sequence represents the number of the rightmost point of the node i of the longest palindromic sequence represented by the end of the palindromic sequence int len [MAXN]; // len [i] indicates the length of a palindromic sequence represented by node i (a node represents a palindromic sequence) int S [MAXN]; // store the added character int Last; // points to the node longest palindromic sequence represented by a letter to add the new formed. int n-; // represents the number of characters to add. int P; // represents the number of nodes added. int newNode ( int L) { // create a new node for ( int I = 0 ; I <N; I ++) NE [P] [I] = 0 ; CNT [P] = NUM [P] = 0 ; len [P] = L; return P ++ ; } void the init () { // Initialization P = 0 ; newNode ( 0 ); newNode ( - . 1 ); // order not anti Last = 0 ; n- = 0 ; S [n-] = - . 1 ; // to prevent cross-border Fail [ 0 ] = . 1 ; } int get_fail ( int X) { the while (S [n-- len [X] - . 1!] = S [n-]) = X Fail [X]; return X; } void the Add ( int C) { C = C - ' A ' ; S [ ++ n-] = C; int CUR = get_fail (Last) ; // by a palindromic sequence to find the matching position palindrome string IF (! NE [CUR] [C]) { // If there have been no palindromic sequence, indicate that there is a new different nature back text strings int now newNode = (len [CUR] + 2 ); // New nodes fail [now] NE = [get_fail (fail [CUR])] [C]; // and AC machines as automatic fail to establish a pointer to after the jump mismatch NE [CUR] [C] = now; NUM [now]NUM = [Fail [now]] + . 1 ; } Last = NE [CUR] [C]; CNT [Last] ++ ; } void count_cnt () { for ( int I = p- . 1 ; I> = 0 ; I - ) { CNT [Fail [I]] + = CNT [I]; // father son accumulated cnt, because if the fail [v] = u, then u must be palindromic sequence sub v! } } } PAM; char ST [MAXN]; int main () { Scanf ( " % S " , ST); int len = strlen (ST); pam.init(); for(int i = 0; i < len; i++){ pam.add(st[i]); } pam.count_cnt(); ll ma = 0; for(int i = 2; i < pam.p; i++){ ma = max((ll)pam.len[i] * pam.cnt[i], ma); } cout << ma <<endl; return 0; }