前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >loj#6031. 「雅礼集训 2017 Day1」字符串(SAM 广义SAM 数据分治)

loj#6031. 「雅礼集训 2017 Day1」字符串(SAM 广义SAM 数据分治)

作者头像
attack
发布2019-03-19 17:11:53
5980
发布2019-03-19 17:11:53
举报

题意

链接

Sol

10^5次询问每次询问10^5个区间。。这种题第一感觉就是根号/数据分治的模型。

K是个定值这个很关键。

考虑K比较小的情况,可以直接暴力建SAM,n^2枚举w的子串算出现次数。询问用个n^2的vector记录一下每次在vector里二分就好。

K比较大的情况我没想到什么好的做法,网上的做法复杂度也不是很好。。

然后写了个广义SAM + 暴力跳parent就过了。。

不过这题思想还是很好的

代码语言:javascript
复制
#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long 
#define ull unsigned long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 4e5 + 10, INF = 1e9 + 1, mod = 1e9 + 7;
const double eps = 1e-9, pi = acos(-1);
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, M, Q, K;
char s[MAXN], w[MAXN];
string q[MAXN];
int l[MAXN], r[MAXN], a[MAXN], b[MAXN], pos[MAXN];
vector<int> line[1001][1001], v[MAXN];
int ch[MAXN][26], siz[MAXN], len[MAXN], fa[MAXN], las = 1, root = 1, tot = 1;
void insert(int x, int opt) {
    int now = ++tot, pre = las; las = now; len[now] = len[pre] + 1; siz[now] = opt; 
    for(; pre && !ch[pre][x]; pre = fa[pre]) ch[pre][x] = now;
    if(!pre) fa[now] = root; 
    else {
        int q = ch[pre][x];
        if(len[pre] + 1 == len[q]) fa[now] = q;
        else {
            int nq = ++tot; len[nq] = len[pre] + 1; fa[nq] = fa[q];
            memcpy(ch[nq], ch[q], sizeof(ch[q]));
            fa[now] = fa[q] = nq;
            for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nq;
        }
    }
}
void dfs(int x) {
    for(auto &to : v[x]) {
        dfs(to);
        siz[x] += siz[to];
    }
}
int Query(vector<int> &q, int a, int b) {
    return (upper_bound(q.begin(), q.end(), b) - lower_bound(q.begin(), q.end(), a));
}
LL solve1(int a, int b) {
    LL ret = 0;
    for(int i = 1; i <= K; i++) {
        int now = root;
        for(int j = i; j <= K; j++) {
            int x = w[j] - 'a'; now = ch[now][x];
            if(!now) break;
            else ret += 1ll * siz[now] * Query(line[i][j], a, b);
        }
    }
    return ret;
}
void Build() {
    for(int i = 1; i <= tot; i++) v[fa[i]].push_back(i);
    dfs(root);
}
signed main() {
//  freopen("string9.in", "r", stdin);  freopen("b.out", "w", stdout);
    N = read(); M = read(); Q = read(); K = read(); 
    scanf("%s", s + 1);
    for(int i = 1; i <= N; i++) insert(s[i] - 'a', 1);
    if(K <= 1000) {
        Build();
        for(int i = 1; i <= M; i++) l[i] = read() + 1, r[i] = read() + 1, line[l[i]][r[i]].push_back(i);
        for(int i = 1; i <= Q; i++) {
            scanf("%s", w + 1); int a = read() + 1, b = read() + 1;
            cout <<  solve1(a, b) << '\n';
        }
    } 
    else {
        for(int i = 1; i <= M; i++) l[i] = read() + 1, r[i] = read() + 1;
        for(int i = 1; i <= Q; i++) {
            cin >> q[i]; a[i] = read() + 1, b[i] = read() + 1;
            las = 1;
            for(auto &x : q[i]) insert(x - 'a', 0);
        }
        Build();
        for(int i = 1; i <= Q; i++) {
            memset(pos, 0, sizeof(pos));
            int now = root;
            for(int j = 0; j < q[i].length(); j++) {
                int x = q[i][j] - 'a'; now = ch[now][x];
                if(!now) break;
                else pos[j + 1] = now;
            }
            LL ans = 0;
            for(int j = a[i]; j <= b[i]; j++) {
                int cur = pos[r[j]];
                while(len[fa[cur]] >= r[j] - l[j] + 1) cur = fa[cur];//这里可以卡掉
                ans += siz[cur];
            }
            cout << ans << '\n';
        }
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档