Codeforces Gym 100548G The Problem to Slow Down You (Palindromic Tree 或 Hash水过) 2014西安现场赛G题

题目大意:

就是现在给你两个长度不超过20W的字符串, 都只包含小写字母, 求相同的回文串对数 (S, T), 其中S == T, S来自第一个字符串, T来自第二个字符串, S和T都是回文串


大致思路:

首先很容易想到的是Manacher + 后缀数组二分 + Hash的做法, 复杂度O(nlogn), 可惜的是这个题目Hash容易被卡

试了好几次Hash之后试了一发二次Hash居然神奇地过了= =...不过这题用Hash还是太靠RP了, 而且幸好时间放的宽到了20s

另外一个正确的解法是用回文树Palindromic Tree, 对于串A, B分别建立回文树, 然后从根节点(奇数的根节点和偶数根节点分别进行一次) dfs, 如果有相同的回文串必然存在相同的代表结点, 其根节点到那个回文的结点的路径也是相同的

于是两棵树同时遍历, 因为Palindromic Tree可以在O(n)的时间复杂度中处理出每种回文串的个数, 所以计数就很方便了, 另外由于回文个数在O(n)以内, dfs复杂度也是O(n)

所以在O(n)的复杂度解出这题就很容易了


两种方法的代码如下:

Manacher + 后缀数组二分 + Hash的做法 ( RP因素较高 ) :

( Hash大法好, Hash出奇迹(>_<) )

Result  :  Accepted     Memory  :  66000 KB     Time  :  9984 ms

/*
 * Author: Gatevin
 * Created Time:  2015/3/31 14:25:53
 * File Name: Rin_Tohsaka.cpp
 */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const double eps(1e-8);
typedef long long lint;
typedef unsigned long long ulint;

#define maxn 200010

char A[maxn], B[maxn], s[maxn << 1];
int R[maxn << 1];
const ulint mod = 1e9 + 10071;

set > S;

ulint H[maxn], xp[maxn];
ulint H2[maxn], xp2[maxn];
const ulint seed = 300007uLL;
const ulint seed2 = 500009uLL;

void initHash(char *s, int n)
{
    H[0] = (ulint)(s[0] - 'a' + 1);
    for(int i = 1; i < n; i++)
        H[i] = (H[i - 1]*seed % mod + (ulint)(s[i] -'a' + 1)) % mod;
    return;
}

void initHash2(char *s, int n)
{
    H2[0] = (ulint)(s[0] - 'a' + 1);
    for(int i = 1; i < n; i++)
        H2[i] = H2[i - 1]*seed2 + (ulint)(s[i] - 'a' + 1);
    return;
}

ulint askHash(int l, int r)
{
    if(l == 0)
        return H[r];
    else
        return (H[r] - H[l - 1]*xp[r - l + 1] % mod + mod) % mod;
}

ulint askHash2(int l, int r)
{
    if(l == 0)
        return H2[r];
    else
        return H2[r] - H2[l - 1]*xp2[r - l + 1];
}

int wa[maxn], wb[maxn], wv[maxn], Ws[maxn];

int cmp(int *r, int a, int b, int l)
{
    return r[a] == r[b] && r[a + l] == r[b + l];
}

void da(int *r, int *sa, int n, int m)
{
    int *x = wa, *y = wb, *t, i, j, p;
    for(i = 0; i < m; i++) Ws[i] = 0;
    for(i = 0; i < n; i++) Ws[x[i] = r[i]]++;
    for(i = 1; i < m; i++) Ws[i] += Ws[i - 1];
    for(i = n - 1; i >= 0; i--) sa[--Ws[x[i]]] = i;
    for(j = 1, p = 1; p < n; j <<= 1, m = p)
    {
        for(p = 0, i = n - j; i < n; i++) y[p++] = i;
        for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
        for(i = 0; i < n; i++) wv[i] = x[y[i]];
        for(i = 0; i < m; i++) Ws[i] = 0;
        for(i = 0; i < n; i++) Ws[wv[i]]++;
        for(i = 1; i < m; i++) Ws[i] += Ws[i - 1];
        for(i = n - 1; i >= 0; i--) sa[--Ws[wv[i]]] = y[i];
        for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++)
            x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
    }
    return;
}

int rank[maxn], height[maxn];
void calheight(int *r, int *sa, int n)
{
    int i, j, k = 0;
    for(i = 1; i <= n; i++) rank[sa[i]] = i;
    for(i = 0; i < n; height[rank[i++]] = k)
        for(k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++);
    return;
}

int dp[maxn][20];
void initRMQ(int n)
{
    for(int i = 1; i <= n; i++) dp[i][0] = height[i];
    for(int j = 1; (1 << j) <= n; j++)
        for(int i = 1; i + (1 << j) - 1 <= n; i++)
            dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
    return;
}

int askRMQ(int a, int b)
{
    //int ra = rank[a], rb = rank[b];
    int ra = a, rb = b;
    if(ra > rb) swap(ra, rb);
    int k = 0;
    while((1 << (k + 1)) <= rb - ra + 1) k++;
    return min(dp[ra][k], dp[rb - (1 << k) + 1][k]);
}

int calCnt(int l, int r, int n)
{
    int rl = rank[l];
    int lmost = rl, rmost = rl;
    int L = rl + 1, R = n, mid;
    while(L <= R)
    {
        mid = (L + R) >> 1;
        if(askRMQ(rl + 1, mid) >= r - l + 1)
        {
            L = mid + 1;
            rmost = mid;
        }
        else
            R = mid - 1;
    }
    L = 1, R = rl - 1;
    while(L <= R)
    {
        mid = (L + R) >> 1;
        if(askRMQ(mid + 1, rl) >= r - l + 1)
        {
            R = mid - 1;
            lmost = mid;
        }
        else
            L = mid + 1;
    }
    return rmost - lmost + 1;
}

vector  > pal;

void Manacher(char *s, int *R, int n)
{
    int mx = 0, p = 0;
    R[0] = 1;
    S.clear(), pal.clear();
    for(int i = 1; i < n; i++)
    {
        if(mx > i) R[i] = min(R[2*p - i], mx - i);
        else R[i] = 1;
        while(s[i - R[i]] == s[i + R[i]])
            R[i]++;
        if(i + R[i] > mx)
        {
            for(int j = mx; j < i + R[i]; j++)
            {
                int l = 2*i - j, r = j;
                l >>= 1;
                r = (r & 1) ? r >> 1 : (r >> 1) - 1;
                if(l > r) continue;
                ulint hashvalue1 = askHash(l, r);
                ulint hashvalue2 = askHash2(l, r);
                set > :: iterator it = S.find(make_pair(hashvalue1, hashvalue2));
                if(it == S.end())
                {
                    S.insert(make_pair(hashvalue1, hashvalue2));
                    pal.push_back(make_pair(l, r));
                }
            }
            mx = i + R[i], p = i;
        }
    }
    return;
}

map , int> Ma, Mb;
int ss[maxn], sa[maxn];

int main()
{
    xp[0] = 1uLL;
    xp2[0] = 1uLL;
    for(int i = 1; i < maxn; i++)
        xp[i] = xp[i - 1]*seed % mod, xp2[i] = xp2[i - 1]*seed2;
    int T;
    scanf("%d", &T);
    for(int cas = 1; cas <= T; cas++)
    {
        scanf("%s", A);
        scanf("%s", B);
        int la = strlen(A), lb = strlen(B);
        
        initHash(A, la);
        initHash2(A, la);
        s[0] = '@';
        for(int i = 0; i < la; i++)
            s[2*i + 1] = A[i], s[2*i + 2] = '#', ss[i] = A[i] - 'a' + 1;
        s[2*la] = '$';
        ss[la] = 0;
        Manacher(s, R, 2*la);
        da(ss, sa, la + 1, 280);
        calheight(ss, sa, la);
        initRMQ(la);
        Ma.clear();
        for(unsigned int i = 0, sz = pal.size(); i < sz; i++)
            Ma[make_pair(askHash(pal[i].first, pal[i].second), askHash2(pal[i].first, pal[i].second))] = calCnt(pal[i].first, pal[i].second, la);
        
        initHash(B, lb);
        initHash2(B, lb);
        s[0] = '@';
        for(int i = 0; i < lb; i++)
            s[2*i + 1] = B[i], s[2*i + 2] = '#', ss[i] = B[i] -'a' + 1;
        s[2*lb] = '$';
        ss[lb] = 0;
        Manacher(s, R, 2*lb);
        da(ss, sa, lb + 1, 28);
        calheight(ss, sa, lb);
        initRMQ(lb);
        Mb.clear();
        for(unsigned int i = 0, sz = pal.size(); i < sz; i++)
            Mb[make_pair(askHash(pal[i].first, pal[i].second), askHash2(pal[i].first, pal[i].second))] = calCnt(pal[i].first, pal[i].second, lb);
        
        lint ans = 0;
        for(map , int> :: iterator it = Ma.begin(); it != Ma.end(); it++)
            if(Mb[(*it).first] != 0)
                ans += (lint)(*it).second*(lint)Mb[(*it).first];
        printf("Case #%d: %I64d\n", cas, ans);
    }
    return 0;
}



Palindromic Tree的做法:

Result  :  Accepted     Memory  :  53608 KB     Time  :  327 ms

/*
 * Author: Gatevin
 * Created Time:  2015/3/31 17:08:38
 * File Name: Rin_Tohsaka.cpp
 */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define maxn 200010

struct Palindromic_Tree
{
    struct node
    {
        int next[26];
        int len;
        int sufflink;
        int times;//记录这个node代表的回文串出现的次数
    };
    node tree[maxn];
    int L, len, suff;
    char s[maxn];
    void newnode()
    {
        L++;
        for(int i = 0; i < 26; i++)
            tree[L].next[i] = -1;
        tree[L].len = tree[L].sufflink = tree[L].times = 0;
        return;
    }
    void init()
    {
        L = 0, suff = 2;
        newnode(), newnode();
        tree[1].len = -1; tree[1].sufflink = 1;
        tree[2].len = 0; tree[2].sufflink = 1;
        return;
    }
    bool addLetter(int pos)
    {
        int cur = suff, curlen = 0;
        int alp = s[pos] - 'a';
        while(1)
        {
            curlen = tree[cur].len;
            if(pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos])
                break;
            cur = tree[cur].sufflink;
        }
        if(tree[cur].next[alp] != -1)
        {
            suff = tree[cur].next[alp];
            tree[suff].times++;
            return false;
        }
        newnode();
        suff = L;
        tree[L].len = tree[cur].len + 2;
        tree[cur].next[alp] = L;
        if(tree[L].len == 1)
        {
            tree[L].sufflink = 2;
            tree[L].times++;
            return true;
        }
        while(1)
        {
            cur = tree[cur].sufflink;
            curlen = tree[cur].len;
            if(pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos])
            {
                tree[L].sufflink = tree[cur].next[alp];
                break;
            }
        }
        tree[L].times++;
        return true;
    }
    void count()
    {
        for(int i = L; i > 0; i--)
            tree[tree[i].sufflink].times += tree[i].times;
        return;
    }
    void build()
    {
        init();
        scanf("%s", s);
        int length = strlen(s);
        for(int i = 0; i < length; i++)
            addLetter(i);
        count();
        return;
    }
};

Palindromic_Tree A, B;

/*
 * dfs从两份树的奇偶根节点开始向下, 有相同的回文串就加上数量乘积
 * 只有有相同的才继续向下找
 */
lint dfs(int nowA, int nowB)
{
    lint ret = 0;
    for(int i = 0; i < 26; i++)
        if(A.tree[nowA].next[i] != -1 && B.tree[nowB].next[i] != -1)
            ret += (lint)A.tree[A.tree[nowA].next[i]].times * (lint)B.tree[B.tree[nowB].next[i]].times
                + dfs(A.tree[nowA].next[i], B.tree[nowB].next[i]);
    return ret;
}

int main()
{
    int T;
    scanf("%d", &T);
    for(int cas = 1; cas <= T; cas++)
    {
        A.build();
        B.build();
        lint ans = dfs(1, 1) + dfs(2, 2);
        printf("Case #%d: %I64d\n", cas, ans);
    }
    return 0;
}



你可能感兴趣的:(Codeforces,ACM_Manacher,ACM_Hash)