POJ 3415 后缀数组 + 单调栈

简略题意:求两个串长度不小于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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ 2182是一道使用树状数组解决的题目,题目要求对给定的n个数进行排序,并且输出每个数在排序后的相对位置。树状数组是一种用来高效处理前缀和问题的数据结构。 根据引用中的描述,我们可以通过遍历数组a,对于每个元素a[i],可以使用二分查找找到a到a[i-1]中小于a[i]的数的个数。这个个数就是它在排序后的相对位置。 代码中的query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。 最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。 参考代码如下: ```C++ #include <iostream> #include <cstdio> using namespace std; int n, a += y; } } int main() { scanf("%d", &n); f = 1; for (int i = 2; i <= n; i++) { scanf("%d", &a[i]); f[i = i & -i; } for (int i = n; i >= 1; i--) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; int k = query(mid - 1); if (a[i > k) { l = mid + 1; } else if (a[i < k) { r = mid - 1; } else { while (b[mid]) { mid++; } ans[i = mid; b[mid = true; add(mid, -1); break; } } } for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; } ``` 这段代码使用了树状数组来完成题目要求的排序功能,其中query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [poj2182Lost Cows——树状数组快速查找](https://blog.csdn.net/aodan5477/article/details/102045839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [poj_2182 线段树/树状数组](https://blog.csdn.net/weixin_34138139/article/details/86389799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值