欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Anti-Rhyme Pairs 【UVA - 12338】【后缀数组】

程序员文章站 2022-04-17 14:07:18
...

题目链接


  题意:有N个字符串,现在给出了Q次查询,询问的是第i个字符串和第j个字符串最长相等的前缀有多长?

  思路:直接上后缀数组,我们知道了每个字符串的起点的下标,然后我们可以通过返回其rank值的办法来知晓它在SA中的所在的位置,然后直接查区间的最小值即可。时间复杂度Anti-Rhyme Pairs 【UVA - 12338】【后缀数组】级别。

  对了,千万要开够内存空间,因为这道题确确实实的卡了RE,千万要开够。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e6 + 1e5 + 7;
struct SA
{
    int n, m;
    int s[maxN];
    int y[maxN], x[maxN], c[maxN], sa[maxN], rk[maxN], height[maxN];
    inline void get_SA()
    {
        for(int i=1; i<=m; i++) c[i] = 0;   //桶的初始化
        for(int i=1; i<=n; i++) ++c[x[i] = s[i]];
        for(int i=2; i<=m; i++) c[i] += c[i - 1];   //利用差分前缀和的思想知道每个关键字最多是在第几名
        for(int i=n; i>=1; i--) sa[c[x[i]]--] = i;
        for(int k=1; k<=n; k<<=1)
        {
            int num = 0;
            for(int i=n - k + 1; i<=n; i++) y[++num] = i;
            for(int i=1; i<=n; i++) if(sa[i] > k) y[++num] = sa[i] - k; //是否可以作为第二关键字
            for(int i=1; i<=m; i++) c[i] = 0;
            for(int i=1; i<=n; i++) c[x[i]]++;  //因为上一次循环已经求出这次的第一关键字了
            for(int i=2; i<=m; i++) c[i] += c[i - 1];
            for(int i=n; i>=1; i--) //在同一第一关键字下,按第二关键字来排
            {
                sa[c[x[y[i]]]--] = y[i];
                y[i] = 0;
            }
            swap(x, y);
            x[sa[1]] = 1; num = 1;
            for(int i=2; i<=n; i++)
            {
                x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? num : ++num;
            }
            if(num == n) break;
            m = num;
        }
    }
    inline void get_height()
    {
        int k = 0;
        for(int i=1; i<=n; i++) rk[sa[i]] = i;
        for(int i=1; i<=n; i++)
        {
            if(rk[i] == 1) continue;    //第一名的height为0
            if(k) k--;  //height[i] >= height[i - 1] - 1
            int j = sa[rk[i] - 1];
            while(j + k <= n && i + k <= n && s[i + k] == s[j + k]) k++;
            height[rk[i]] = k;
        }
    }
    inline void clear()
    {
        n = 0; m = 2e5;
    }
} sa;
struct BIT_Tree
{
    int tree[maxN << 2];
    inline void buildTree(int rt, int l, int r)
    {
        if(l == r) { tree[rt] = sa.height[l]; return; }
        int mid = HalF;
        buildTree(Lson); buildTree(Rson);
        tree[rt] = min(tree[lsn], tree[rsn]);
    }
    inline int query(int rt, int l, int r, int ql, int qr)
    {
        if(ql <= l && qr >= r) return tree[rt];
        int mid = HalF;
        if(qr <= mid) return query(QL);
        else if(ql > mid) return query(QR);
        else return min(query(QL), query(QR));
    }
} tree;
int N, Q, str_beg[maxN], str_end[maxN], len;
char s[10005];
int main()
{
    int T; scanf("%d", &T);
    for(int Cas=1; Cas <= T; Cas++)
    {
        sa.clear();
        scanf("%d", &N);
        for(int i=1; i<=N; i++)
        {
            str_beg[i] = sa.n + 1;
            scanf("%s", s + 1);
            len = (int)strlen(s + 1);
            for(int j=1; j<=len; j++) sa.s[++sa.n] = s[j];
            sa.s[++sa.n] = 'a' + 25 + i;
            str_end[i] = sa.n;
        }
        sa.get_SA();
        sa.get_height();
        tree.buildTree(1, 2, sa.n - N);
        scanf("%d", &Q);
        int l, r, ii, jj;
        printf("Case %d:\n", Cas);
        while(Q--)
        {
            scanf("%d%d", &ii, &jj);
            l = sa.rk[str_beg[ii]]; r = sa.rk[str_beg[jj]];
            if(l > r) swap(l, r);
            if(l ^ r) printf("%d\n", tree.query(1, 2, sa.n - N, l + 1, r));
            else printf("%d\n", str_end[jj] - str_beg[jj]);
        }
    }
    return 0;
}
/*
2
5
daffodilpacm
daffodiliupc
distancevector
distancefinder
distinctsubsequence
4
1 2
1 5
3 4
4 5
2
acm
icpc
2
1 2
2 2
*/