hdu3948——后缀数组统计不同回文子串的个数

    构造一个新的字符串,由3部分组成:第一部分为原始字符串;第二部分是一个从未出现过的分隔符;第三部分为原始串的倒序。对该串求sa、rank、height数组,然后遍历height数组(即枚举该串所有的后缀),对于起始位置在第一部分的后缀,求出该后缀与其在第三部分对应起始位置的后缀的LCP的长度(记为lcp_len(i)),该长度就是新增的回文子串的个数;其实就是枚举回文串的中间位置,利用height数组的性质和经过了rmp问题预处理后,可以在O(1)的时间求出以该位置为中心的回文子串的个数。

    对于去重的问题,和统计不同子串个数的方法相类似,也是利用height数组的性质。对于height[i]对应的后缀,对其求得的新增的回文子串中有多少是计算过的呢?用一个变量动态记录这个值,然后根据height[i]和lcp_len(i)去更新。详见代码。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int maxn = 400000 + 10;
int r[maxn], n, len;
int sa[maxn], t[maxn], t2[maxn], c[maxn];
int rank[maxn], height[maxn];
char str[maxn];
int rmp[maxn][50], log2[maxn];

void build_sa(int * s, int n, int m)
{
    int i, * x = t, * y = t2, * t;
    for(i = 0; i < m; ++i) c[i] = 0;
    for(i = 0; i < n; ++i) c[x[i] = s[i]]++;
    for(i = 1; i < m; ++i) c[i] += c[i - 1];
    for(i = n - 1; i >= 0; --i) sa[--c[x[i]]] = i;
    for(int k = 1; k <= n; k <<= 1)
    {
        int p = 0;
        for(i = n - k; i < n; ++i) y[p++] = i;
        for(i = 0; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k;
        for(i = 0; i < m; ++i) c[i] = 0;
        for(i = 0; i < n; ++i) c[x[y[i]]]++;
        for(i = 1; i < m; ++i) c[i] += c[i - 1];
        for(i = n - 1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i];
        t = x; x = y; y = t;
        p = 1; x[sa[0]] = 0;
        for(i = 1; i < n; ++i)
            x[sa[i]] = (y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]) ? p - 1 : p++;
        if(p >= n) break;
        m = p;
    }
}

void get_heigth(int * s, int n)
{
    int i, j, k = 0;
    for(i = 1; i <= n; ++i) rank[sa[i]] = i;
    for(i = 0; i < n; ++i)
    {
        if(k) k--;
        j = sa[rank[i] - 1];
        while(s[i + k] == s[j + k]) k++;
        height[rank[i]] = k;
    }
}

void rmp_init()
{
    for(int i = 1; i <= n; ++i) rmp[i][0] = height[i];
    for(int j = 1; (1 << j) <= n; ++j)
        for(int i = 1; i + j - 1 <= n; ++i)
            rmp[i][j] = rmp[i][j - 1] < rmp[i + (1 << (j - 1))][j - 1] ? rmp[i][j - 1] : rmp[i + (1 << (j - 1))][j - 1];
}

int get_rmp(int l, int r)
{
    int k = log2[r - l + 1];
    return rmp[l][k] < rmp[r - (1 << k) + 1][k] ? rmp[l][k] : rmp[r - (1 << k) + 1][k];
}

int main()
{
    freopen("in.txt", "r",stdin);
    int t; scanf("%d", &t); int cc = 1;
    int num = 0, tot = 1;
    while(tot < maxn)
    {
        for(int i = tot; i < tot * 2 && i < maxn; ++i)
            log2[i] = num;
        num++; tot *= 2;
    }
    while(t--)
    {
        scanf("%s", str); n = 0; len = strlen(str);
        for(int i = 0; i < len; ++i) r[n++] = str[i] - 'a' + 1;
        r[n++] = 28;
        for(int i = len - 1; i >= 0; --i) r[n++] = str[i] - 'a' + 1;
        r[n] = 0;
        build_sa(r, n + 1, 30); get_heigth(r, n);
        rmp_init();
        int res = 0, cnt = 0;
        for(int i = 1; i < n; ++i)
        {
            cnt = (cnt > height[i]) ? height[i] : cnt;
            if(sa[i] < len)
            {
                int other = 2 * len - sa[i];
                int tem = get_rmp(min(i, rank[other]) + 1, max(i, rank[other]));
                if(tem > cnt)
                {
                    res += (tem - cnt);
                    cnt = tem;
                }
            }
        }
        cnt = 0;
        for(int i = 1; i < n; ++i)
        {
            cnt = (cnt > height[i]) ? height[i] : cnt;
            if(!sa[i]) continue;
            if(sa[i] < len)
            {
                int other = 2 * len - sa[i] + 1;
                int tem = get_rmp(min(i, rank[other]) + 1, max(i, rank[other]));
                if(tem > cnt)
                {
                    res += tem - cnt;
                    cnt = tem;
                }
            }
        }
        printf("Case #%d: %d\n", cc++, res);
    }
    return 0;
}
/*
3
aaaa
abab
abcd

Case #1: 4
Case #2: 4
Case #3: 4
*/


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值