2019暑期杭电多校HDU6599 I Love Palindrome String

2019杭电多校HDU6599 I Love Palindrome String


题目链接

I Love Palindrome String
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 582 Accepted Submission(s): 220
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

Source
2019 Multi-University Training Contest 2

题意:
给你一个字符串,求对于每一个长度i-len,问有多少个,回文串的前一半也是回文串
思路:
回文树裸题,用hash判断前后两部分是否相同

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", (n))
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define mm(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const int MAXN = 100005 ;
const int N = 3e5+10;
const ull hash1 = 201326611;
const ull hash2 = 50331653;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret*10 + ch - '0';
        ch = getchar();
    }
    return ret*sgn;
}
char str[N];
ll ret[N];
ull ha[N], pp[N];
ull getha(int l, int r)
{
    if (l == 0) return ha[r];
    return ha[r] - ha[l - 1] * pp[r - l + 1];
}
bool check(int l, int r)
{
    int len = r - l + 1;
    int mid = (l + r) >> 1;
    if (len & 1) return getha(l, mid) == getha(mid, r);
    else return getha(l, mid) == getha(mid + 1, r);
}
struct PTree
{
    int nxt[N][26]; //i节点对应的回文串在两边各加一个字符后变成的节点编号
    int fail[N]; //fail[i]表示的串是i的最长后缀回文串
    int cnt[N]; //表示这个串出现过几次
    int num[N]; //节点i这个串的后缀有多少是回文的
    int len[N]; //节点i表示的回文串的长度
    int S[N]; //S[i]是第i次添加的字符
    int last; //以n结束的最长回文串所在的节点
    int n; //目前添加的字符个数
    int p; //下一个新建节点的标号
    int id[N];
    inline int newnode(int l)
    {
        //新建节点
        for(int i = 0; i < 26; ++i) nxt[p][i] = 0;
        cnt[p] = num[p] = 0;
        len[p] = l;
        return p++;
    }
    inline void init()
    {
        p = 0;
        newnode(0);
        newnode(-1);
        last = n = 0;
        S[n] = -1;
        fail[0] = 1;
    }
    inline int get_fail(int x)
    {
        while(S[n - len[x] - 1] != S[n]) x = fail[x];
        return x;
        /*新加进来一个字符时,只有Str[Nnow−len[x]−1]=Str[Nnow]Str[Nnow−len[x]−1]=Str[Nnow]
        我们才可以说是能构成一个新的回文串(构成回文节点唯一方法,就是在已有基础上找一个节点,
        在其两端各拓展一个字符,也就是我们所说的找到一个x,使式子成立)。
        于是我们就可以去不停给last跑fail,直到满足条件。*/
    }
    inline void add(int c)//加一个字符
    {
        c-='a';
        S[++n] = c;
        int cur = get_fail(last); //通过上一个回文串找这个串的匹配位置
        if(!nxt[cur][c])
        {
            //这个回文串从未出现过
            int now = newnode(len[cur] + 2);
            fail[now] = nxt[get_fail(fail[cur])][c]; //和AC自动机简直不要太类似
            nxt[cur][c] = now;
            num[now] = num[fail[now]] + 1;
        }
        last = nxt[cur][c];
        ++cnt[last];
        id[last] = n;//
    }
    inline void count1()//计数
    {
        for(int i = p - 1; i >= 0; --i) cnt[fail[i]]+= cnt[i];
        for (int i = 2; i < p; i++)
        {
            ///cout << id[i] - len[i] << " " << id[i] - 1 << endl;
            if (check(id[i] - len[i], id[i] - 1))
            {
                ret[len[i]] += cnt[i];
            }
        }
    }

    inline void work()
    {
        pp[0] = 1;
        for (int i = 1; i < N; i++)
        {
            pp[i] = hash1 * pp[i - 1];
        }
        memset(ret, 0, sizeof(ret));
        init();
        int slen = strlen(str);
        ha[0] = str[0];
        for (int i = 0; i < slen; i++)
        {
            add(str[i]);
        }
        for (int i = 1; i < slen; i++)
        {
            ha[i] = ha[i - 1] * hash1 + str[i];
        }
        count1();

        printf("%lld", ret[1]);
        for (int i = 2; i <= slen; i++)
        {
            printf(" %lld", ret[i]);
        }
        printf("\n");
    }

} pam ;
int main()
{
    while(~ss(str))
        pam.work();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值