P5496 [template] palindrome automata (PAM)

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/102770060

Title Description

Given a string s s . To ensure that each character is a lowercase letter. Ss for each position, the number of the request to the end of the palindromic substring position.

This string is encrypted, except for the first character, other characters need to be decrypted by the answers on a location.

Specific areas, young first i ( i 1 ) i (i \ geq 1) The answer is location k k , first i + 1 i+1 characters read in the A S C I I ASCII code c c , subsection i + 1 i+1 character real A S C I I ASCII code ( c 97 + k ) m The d 26 + 97 (C-97 + k) \ way 26 + 97 . All characters before and after encryption are all lowercase letters.

Input Format

A string line s s represents a string is encrypted.

Output Format

Line, s |s| integers. The first i i integer represents the original string to the first i i The number of characters in the substring palindrome end.

Palindrome tree template title, but by force, we need to use the front of the palindromic sequence number k

#include<bits/stdc++.h>
using namespace std;
const int MAXNODE = 6e5+50;
const int MOD = 1e9+7;
struct Palindrome{
    int nxt[MAXNODE][26],fail[MAXNODE],len[MAXNODE],cnt[MAXNODE];
    int sz,last,sn;
    char s[MAXNODE];
    Palindrome(){
        len[0]=0,len[1]=-1;
        fail[0]=1,fail[1]=0;
        last = sn = 0,sz = 1;
        s[0] = '#';//将第一个字符表示成字符串中没出现过的字符
    }
    int Build(char ch){
        s[++sn] = ch;
        int root = last;
        while(s[sn-len[root]-1]!=ch)
            root = fail[root];
        if(!nxt[root][ch-'a']){
            len[++sz] = len[root] + 2;
            int tmp = fail[root];
            while(s[sn-len[tmp]-1]!=ch)
                tmp = fail[tmp];
            fail[sz] = nxt[tmp][ch-'a'];
            cnt[sz] = cnt[fail[sz]] + 1;
            nxt[root][ch-'a'] = sz;
        }
        last = nxt[root][ch-'a'];
        return cnt[last];
    }
}PAM;
char str[MAXNODE];
int main(){
    scanf("%s",str);
    int len = strlen(str);
    for(int i=0,k=0;i<len;i++){
        str[i] = (str[i]-97+k)%26+97;
        k = PAM.Build(str[i]);
        printf("%d ",k);
    }
    return 0;
}

Guess you like

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