P5555 Order curse

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43544481/article/details/102770274

Meaning of the questions: give you two strings to determine the longest common substring palindrome length and number two strings

Solution: We can build these two strings are palindromic tree, then simultaneously DFS, take the same transfer edge is obviously obtained the same palindrome sequence, because the length of palindromic sequence may also be an odd number is an even number so we need to root for odd and even root node dfs, you can find the longest common substring length palindrome, as to the number, because the palindrome tree, each palindrome string must be different, so it different statistics may be the longest common substring of the number of palindromic

#include<bits/stdc++.h>
using namespace std;
const int MAXNODE = 3e5+50;
int Max = 0,tot = 0;
struct Palion{
    int nxt[MAXNODE][26],fail[MAXNODE],len[MAXNODE],cnt[MAXNODE];
    int sz,lst;
    char s[MAXNODE];
    void Init(char *str){
        sz = lst = 1;
        len[1] = -1,len[0] = 0;
        fail[0] = 1,fail[1] = 0;
        for(int i=1;str[i];i++) s[i]=str[i];
    }
    void Insert(char ch,int en){
        int root = lst;
        while(s[en]!=s[en-len[root]-1]) root = fail[root];
        if(!nxt[root][ch-'a']){
            len[++sz] = len[root] + 2;
            int tmp = fail[root];
            while(s[en]!=s[en-len[tmp]-1]) tmp = fail[tmp];
            fail[sz] = nxt[tmp][ch-'a'];
            cnt[sz] = cnt[fail[sz]] + 1;
            nxt[root][ch-'a'] = sz;
        }
        lst = nxt[root][ch-'a'];
    }
}PAM1,PAM2;
void dfs(int x,int y){
    if(Max < PAM1.len[x]) Max = PAM1.len[x],tot = 1;
    else if(Max == PAM1.len[x]) ++tot;
    for(int i=0;i<26;i++)
        if(PAM1.nxt[x][i] && PAM2.nxt[y][i])
            dfs(PAM1.nxt[x][i],PAM2.nxt[y][i]);
}
int n,m;
char str1[MAXNODE],str2[MAXNODE];
int main(){
    scanf("%d%d",&n,&m);
    scanf("%s%s",str1+1,str2+1);
    PAM1.Init(str1),PAM2.Init(str2);
    for(int i=1;str1[i];i++) PAM1.Insert(str1[i],i);
    for(int i=1;str2[i];i++) PAM2.Insert(str2[i],i);
    dfs(0,0),dfs(1,1);
    printf("%d %d\n",Max,tot);
}

Guess you like

Origin blog.csdn.net/qq_43544481/article/details/102770274