POJ 3415 后缀数组 + 单调栈

来源:互联网 发布:grub windows引导项 编辑:程序博客网 时间:2024/06/05 22:43

简略题意:求两个串长度不小于k的公共子串的个数。
我喜欢这题!
首先按height分组,随后对于每个A后缀,看之前出现的B后缀与其的LCP,若其长度为x,则对答案的贡献为xk+1。暴力查找n2, 其实B后缀的排名越接近当前A后缀,两者的LCP越高 想一想,为什么, 因此维护一个单调栈,以及栈内元素贡献总和。显然从栈底到栈顶元素逐渐增大。对A统计完答案之后再对B统计一次即可。

#include <iostream>#include <cstring>#include <map>#include <cstdio>#include <vector>#include <algorithm>using namespace std;typedef long long LL;const LL N = 220000;LL n, k;char str[N], str2[N];LL f = 0;namespace SA {    LL sa[N], rank[N], height[N], s[N<<1], t[N<<1], p[N], cnt[N], cur[N];    LL MIN[N][30];    #define pushS(x) sa[cur[s[x]]--] = x    #define pushL(x) sa[cur[s[x]]++] = x    #define inducedSort(v) fill_n(sa, n, -1); fill_n(cnt, m, 0);                  \        for (LL i = 0; i < n; i++) cnt[s[i]]++;                                  \        for (LL i = 1; i < m; i++) cnt[i] += cnt[i-1];                           \        for (LL i = 0; i < m; i++) cur[i] = cnt[i]-1;                            \        for (LL i = n1-1; ~i; i--) pushS(v[i]);                                  \        for (LL i = 1; i < m; i++) cur[i] = cnt[i-1];                            \        for (LL i = 0; i < n; i++) if (sa[i] > 0 &&  t[sa[i]-1]) pushL(sa[i]-1); \        for (LL i = 0; i < m; i++) cur[i] = cnt[i]-1;                            \        for (LL i = n-1;  ~i; i--) if (sa[i] > 0 && !t[sa[i]-1]) pushS(sa[i]-1)    void sais(LL n, LL m, LL *s, LL *t, LL *p) {        LL n1 = t[n-1] = 0, ch = rank[0] = -1, *s1 = s+n;        for (LL i = n-2; ~i; i--) t[i] = s[i] == s[i+1] ? t[i+1] : s[i] > s[i+1];        for (LL i = 1; i < n; i++) rank[i] = t[i-1] && !t[i] ? (p[n1] = i, n1++) : -1;        inducedSort(p);        for (LL i = 0, x, y; i < n; i++) if (~(x = rank[sa[i]])) {            if (ch < 1 || p[x+1] - p[x] != p[y+1] - p[y]) ch++;            else for (LL j = p[x], k = p[y]; j <= p[x+1]; j++, k++)                if ((s[j]<<1|t[j]) != (s[k]<<1|t[k])) {ch++; break;}            s1[y = x] = ch;        }        if (ch+1 < n1) sais(n1, ch+1, s1, t+n, p+n1);        else for (LL i = 0; i < n1; i++) sa[s1[i]] = i;        for (LL i = 0; i < n1; i++) s1[i] = p[sa[i]];        inducedSort(s1);    }    template<typename T>    LL mapCharToLL(LL n, const T *str) {        LL m = *max_element(str, str+n);        fill_n(rank, m+1, 0);        for (LL i = 0; i < n; i++) rank[str[i]] = 1;        for (LL i = 0; i < m; i++) rank[i+1] += rank[i];        for (LL i = 0; i < n; i++) s[i] = rank[str[i]] - 1;        return rank[m];    }    template<typename T>    void suffixArray(LL n, const T *str) {        LL m = mapCharToLL(++n, str);        sais(n, m, s, t, p);        for (LL i = 0; i < n; i++) rank[sa[i]] = i;        for (LL i = 0, h = height[0] = 0; i < n-1; i++) {            LL j = sa[rank[i]-1];            while (i+h < n && j+h < n && s[i+h] == s[j+h]) h++;            if (height[rank[i]] = h) h--;        }    }    void RMQ_init(){        for(LL i=0; i<n; i++) MIN[i][0] = height[i+1];        for(LL j=1; (1<<j)<=n; j++){            for(LL i=0; i+(1<<j)<=n; i++){                MIN[i][j] = min(MIN[i][j-1], MIN[i+(1<<(j-1))][j-1]);            }        }    }    LL RMQ(LL L, LL R){        LL k = 0;        while((1<<(k+1)) <= R-L+1) k++;        return min(MIN[L][k], MIN[R-(1<<k)+1][k]);    }    LL LCP(LL i, LL j){        if(rank[i] > rank[j]) swap(i, j);        return RMQ(rank[i], rank[j]-1);    }    void init(char *str){        suffixArray(n, str);    }    LL stk[N], count[N], top;    void solve(LL k, LL pos) {        top = 0;        LL ans = 0, sum = 0;        for(LL i = 2; i <= n; i++) {            if(height[i] < k) {                top = 0;                sum = 0;                continue;            }            LL cnt = 0;            while(top && stk[top] > height[i]) {                sum -= (stk[top] - k + 1) * count[top];                cnt += count[top];                top--;            }            if(sa[i-1] < pos) {                stk[++top] = height[i];                count[top] = cnt + 1;                sum += (stk[top] - k + 1)*count[top];            } else if(cnt)                stk[++top] = height[i], count[top] = cnt, sum += (stk[top] - k + 1)*count[top];            if(sa[i] > pos)                ans += sum;        }        top = sum = 0;        for(LL i = 2; i <= n; i++) {            if(height[i] < k) {                top = 0;                sum = 0;                continue;            }            LL cnt = 0;            while(top && stk[top] > height[i]) {                sum -= (stk[top] - k + 1) * count[top];                cnt += count[top];                top--;            }            if(sa[i-1] > pos) {                stk[++top] = height[i];                count[top] = cnt + 1;                sum += (stk[top] - k + 1)*count[top];            } else if(cnt)                stk[++top] = height[i], count[top] = cnt, sum += (stk[top] - k + 1)*count[top];            if(sa[i] < pos)                ans += sum;        }        cout<<ans<<endl;    }};int main() {    while(~scanf("%lld", &k) && k) {        scanf("%s", str);        scanf("%s", str2);        LL x = strlen(str);        str[x] = '@'; str[x+1] = 0;        strcat(str, str2);        n = strlen(str);        SA::init(str);        SA::solve(k, x);    };    return 0;}
阅读全文
0 0
原创粉丝点击