poj 2758(后缀数组)

题意:给出一个长度为n的字母组成的序列,然后有两种操作,Q a b 询问以第a个字符为开头的后缀字符串和以第b个字符为开头的后缀字符串的lcp(最长公共前缀)的长度,I a b 表示在第b个字符之前插入字符a。
注意询问时的位置a和b是对应初始串,而比对的后缀串是当前串。
题解:思路想到了,但实现不出来 %>_<%,如果不考虑I操作,先用后缀数组处理出sa和rank,然后得到height相邻名次串的公共前缀长,然后找rank[l]+1到rank[r]的height中找最小值就是解。加上I操作就比较麻烦了,要找到l和r后面的且与l和r的距离最近的添加字符,比较这个最近距离和这两个串的lcp值:如果lcp值更小,说明添加的字符不影响他们的lcp,否则就修改l和r的值,同理递归寻找下一个距离最近的添加字符。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 50500;
struct Ins {
    int v, p;
}ins[205];
int wa[N], wb[N], ws[N], wv[N], sa[N];
int rank[N], height[N], s[N], q, cnt, f[N][35];
char str[N];

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 i, j, p, *x = wa, *y = wb, *t;

    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 *= 2, 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 = 0; 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++;
    }
}

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++);
}

void RMQ_init(int cnt) {
    for (int i = 0; i < cnt; i++)
        f[i][0] = height[i];
    for (int j = 1; (1 << j) <= cnt; j++)
        for (int i = 0; i + (1 << j) - 1 < cnt; i++)
            f[i][j] = min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
}

int RMQ(int L, int R) {
    int k = 0;
    while (1 << (k + 1) <= R - L + 1) k++;
    return min(f[L][k], f[R - (1 << k) + 1][k]);
}

void Insert(int val, int pos, int len) {
    int i;
    for (i = 0; i < cnt; i++)
        if (ins[i].p >= pos) break;
        else pos--;
    if (pos >= len) pos = len - 1;
    for (int j = cnt; j > i; j--) ins[j] = ins[j - 1];
    ins[i].v = val, ins[i].p = pos;
    cnt++;
}

int query(int l, int r, int len) {
    int x, y, res = 0;
    for (x = 0; ins[x].p <= l; x++);
    for (y = 0; ins[y].p <= r; y++);
    if (l == r) return (len - l - 1) + (cnt - x - 1);
    while (1) {
        int pos1 = rank[l], pos2 = rank[r];
        if (pos1 > pos2) swap(pos1, pos2);
        int temp = RMQ(pos1 + 1, pos2);
        int dis1 = ins[x].p - l;
        int dis2 = ins[y].p - r;
        int mindis = min(temp, min(dis1, dis2));
        res += mindis, l += mindis, r += mindis;
        if (mindis == dis1 || mindis == dis2) {
            while (ins[x].p == l && ins[y].p == r) {
                if (ins[x].v == ins[y].v)
                    x++, y++, res++;
                else return res;
            }
            while (ins[x].p == l) {
                if (ins[x].v == s[r])
                    x++, r++, res++;
                else return res;
            }
            while (ins[y].p == r) {
                if (ins[y].v == s[l])
                    l++, y++, res++;
                else return res;
            }
        }
        else return res;
    }
    return res;
}

int main() {
    while (scanf("%s", str) == 1) {
        int len = strlen(str), maxx = -1;
        for (int i = 0; i < len; i++) {
            s[i] = str[i] - 'A' + 1;
            maxx = max(maxx, s[i]);
        }
        s[len] = 0;
        DA(s, sa, len + 1, maxx + 10);
        calheight(s, sa, len);
        RMQ_init(len + 1);
        cnt = 0;
        ins[cnt].v = 0;
        ins[cnt++].p = N;
        scanf("%d", &q);
        char op[5];
        int l, r;
        while (q--) {
            scanf("%s", op);    
            if (op[0] == 'Q') {
                scanf("%d%d", &l, &r);
                printf("%d\n", query(l - 1, r - 1, len + 1));
            }
            else {
                scanf("%s%d", op, &l);
                Insert(op[0] - 'A' + 1, l - 1, len + 1);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值