【2019杭电多校第二场1009=HDU6599】I Love Palindrome String(回文树的fail树)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6599

题目:


Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Problem Description

You are given a string S=s1s2..s|S| containing only lowercase English letters. For each integer i∈[1,|S|] , please output how many substrings slsl+1...sr satisfy the following conditions:

 ∙ r−l+1 equals to i.

 ∙ The substring slsl+1...sr is a palindrome string.

 ∙ slsl+1...s⌊(l+r)/2⌋ is a palindrome string too.

|S| denotes the length of string S.

A palindrome string is a sequence of characters which reads the same backward as forward, such as madam or racecar or abba. 

Input

There are multiple test cases.

Each case starts with a line containing a string S(1≤|S|≤3×105) containing only lowercase English letters.

It is guaranteed that the sum of |S| in all test cases is no larger than 4×106.

Output

For each test case, output one line containing |S| integers. Any two adjacent integers are separated by a space.

Sample Input

abababa

Sample Output

7 0 0 0 3 0 0

 

解题思路:


建回文树,在此基础上找出其fail树,对于样例来说,其fail树如下:

【2019杭电多校第二场1009=HDU6599】I Love Palindrome String(回文树的fail树)_第1张图片

fail树有一个重要的性质:fail树上某个节点的所有父辈表示的回文串都是该节点表示的回文串的后缀回文串,且都是以当前字母为结尾的回文串。

样例:abababa

如8:a这个节点,它表示的最长回文串(即以a为结尾的最长回文串)为abababa,这个最长回文串的最长后缀回文串是ababa,即fail树中8:a的上一个节点6:a所表示的最长回文串,同理,aba是节点4:a表示的回文串,a是节点2:a表示的回文串。

因为本身当前节点表示的就已经是个回文串(记这个回文串为ss),若它的一半也是个回文串(记为s),那么在fail树上dfs遍历时,ss出现之前必须要出现s才满足条件(出现s则说明s是个回文串),且s是ss的后缀,故可以只根据len(s)/2出现与否判断ss是否是符合条件的回文串,利用回文树的cnt数组统计最终答案。

⚠️:满足条件的长度为1的回文串数目是输入的字符串的长度,在dfs遍历的时候需要特殊处理,处理的方式多种,选择一种即可。存fail树的vector数组注意多组输入时清空,并注意方法!!

本题也可用回文树+马拉车/hash解决(待补QAQ)

ac代码:


#include 
using namespace std;
const int maxn = 300005;
char ss[maxn];
int len[maxn], fail[maxn], s[maxn], nxt[maxn][26], cnt[maxn], vis[maxn], ans[maxn];
int last, n, p, cur;
vector G[maxn];
int newnode(int l)
{
    for(int i = 0; i < 26; i++)
        nxt[p][i] = 0;
    len[p] = l;
    cnt[p] = 0;
    G[p].clear();// 注意此处清空!!
    return p++;
}
void init()
{
    memset(ans, 0, sizeof(ans));
    memset(vis, 0, sizeof(vis));
    memset(cnt, 0, sizeof(cnt));
    vis[1] = 1;
    last = 0, n = 0, p = 0;
    s[0] = -1;
    newnode(0);
    newnode(-1);
    fail[0] = 1;
    fail[1] = 0;
}
int get_fail(int x)
{
    while(s[n - len[x] - 1] != s[n])
        x = fail[x];
    return x;
}
void insert(int c)
{
    s[++n] = c;
    cur = get_fail(last);
    if(!nxt[cur][c])
    {
        int now = newnode(len[cur] + 2);
        fail[now] = nxt[get_fail(fail[cur])][c];
        nxt[cur][c] = now;
        G[fail[now]].push_back(now);
    }
    last = nxt[cur][c];
    cnt[last]++;
}
void count()
{
    for(int i = p - 1; i >= 0; i--)
        cnt[fail[i]] += cnt[i];
}
void dfs(int x)
{
    vis[1] = 1;//或者不计算长度是1的情况,输出的时候直接输出字符串长度即可
    int half = (int)ceil(len[x]*1.0/2);
    if(vis[half])
        ans[len[x]] += cnt[x];
    vis[len[x]] =  1;// 是++,不是=1 ,
    for(int i = 0; i < G[x].size(); i++)
        dfs(G[x][i]);
    vis[len[x]] = 0;
}
int main()
{
    //freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
    while(~scanf("%s", ss))
    {
        init();
        int leng = strlen(ss);
        for(int i = 0; i < leng; i++)
            insert(ss[i] - 'a');
        count();
        dfs(0);
        for(int i = 1; i < leng; i++)
            printf("%d ", ans[i]);
        printf("%d\n", ans[leng]);
    }
    return 0;
}

【参考博客】:

https://blog.csdn.net/Cymbals/article/details/97391112

你可能感兴趣的:(回文树)