HDU - 3948 The Number of Palindromes 后缀数组+ST表、distinct substring

The Number of Palindromes

  HDU - 3948


Now, you are given a string S. We want to know how many distinct substring of S which is palindrome.
Input
The first line of the input contains a single integer T(T<=20), which indicates number of test cases. 
Each test case consists of a string S, whose length is less than 100000 and only contains lowercase letters. 
Output
For every test case, you should output "Case #k:" first in a single line, where k indicates the case number and starts at 1. Then output the number of distinct substring of S which is palindrome. 
Sample Input
3
aaaa
abab
abcd
Sample Output
Case #1: 4
Case #2: 4
Case #3: 4

Source HDU - 3948
My Solution 题意:给出一个字符串,求出其回文子串的种数。 后缀数组+ST表
把s变成 s + '#' + s',其中s'表示s的反向串。 所以当回文子串的长度为奇数时  [i+1,  _rank[n - sa[i] - 1]] 的height最小值表示以s[sa[i]]为中心的回文子串的最大半径(算上中心);  长度为偶数时 [i+1,  _rank[n - sa[i]]] 的height最小值表示以s[sa[i]]为半径起点的回文子串的最大半径;
找这个最大半径,可以用ST表O(nlogn)的预处理出,然后每次O(1)的询问。 所以跑出height以后,o,e分表表示长度为偶数和奇数的情况下,i-1时的最长半径长度,且o = min(o, height[i]), e = min(e, height[i])。 此时 就是 ans += max(0, maxheight[] - o); ans += max(0, maxheight);然后刷新o,e。 此外注意ST的查询操作要有 if(l > r) return 0;以保证每组构成回文子串的后缀只会查询一次。 复杂度 O(nlogn)
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 8;
int sa[maxn], height[maxn];
int _rank[maxn], t1[maxn], t2[maxn], c[maxn];
string s;
inline void get_sa(const int &n, int m)
{
    int i, k, *x = t1, *y = t2, p, j;
    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(k = 1; k <= n; k <<= 1){
        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];
        swap(x, y), 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;
    }
    k = 0;
    for(i = 0; i < n; i++) _rank[sa[i]] = i;
    for(i = 0; i < n; i++){
        if(k) --k; if(!_rank[i]) continue;
        j = sa[_rank[i] - 1];
        while(s[i + k] == s[j + k]) k++;
        height[_rank[i]] = k;
    }
}

inline void print(const int &n)
{
    for(int i = 1; i <= n; i++){
        cout << i << " : " << _rank[sa[i]] << " " << sa[i] << endl;
        for(int j = sa[i]; j < n; j++){
            cout << s[j];
        }
        cout << endl;
    }
    cout << endl;
}

struct ST_list{
    int stTable[maxn][32], preLog2[maxn];
    inline void st_prepare(const int &n, int arr[]){
        preLog2[1] = 0;
        for(int i = 2; i <= n; i++){
            preLog2[i] = preLog2[i-1];
            if((1 << (preLog2[i] + 1)) == i){
                preLog2[i]++;
            }
        }
        for(int i = n - 1; i >= 0; i--){
            stTable[i][0] = arr[i];
            for(int j = 1; (i + (1 << j) - 1) < n; j++){
                stTable[i][j] = min(stTable[i][j - 1], stTable[i + (1 << j - 1)][j - 1]); //1
            }
        }
    }
    inline int query_min(int l, int r){
        if(l > r) return 0;
        int len = r - l + 1, k = preLog2[len];
        return min(stTable[l][k], stTable[r - (1 << k) + 1][k]);  //2
    }
}st;

int main()
{
    #ifdef LOCAL
    freopen("9.in", "r", stdin);
    //freopen("9.out", "w", stdout);
    #endif // LOCAL
    ios::sync_with_stdio(false); cin.tie(0);

    int T, n, sz, i, o, e, ans, kase = 1;
    cin >> T;
    while(T--){
        cin >> s;
        sz = s.size();
        s += '#';
        for(i = sz - 1; i >= 0; i--) s += s[i];
        n = sz + sz + 1;
        get_sa(n+1, 256); st.st_prepare(n+1, height);
        //print(n);
        ans = 0; o = e = 0;
        for(i = 2; i <= n; i++){
            o = min(o, height[i]);
            ans += max(0, st.query_min(i+1, _rank[n - sa[i] - 1]) - o); o = max(o, st.query_min(i+1, _rank[n - sa[i] - 1]));
            //cout << i << " " << _rank[n - sa[i] - 1] << " " << st.query_min(i, _rank[n - sa[i] - 1]) << endl;
            e = min(e, height[i]);
            ans += max(0, st.query_min(i+1, _rank[n - sa[i]]) - e); e = max(e, st.query_min(i+1, _rank[n - sa[i]]));
            //cout << o << " " << e << " " << ans << endl;
        }
        cout << "Case #" << kase << ": " << ans << "\n"; kase++;

    }
    return 0;
}


Thank you!                                                                                                                                              ------from  ProLights

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值