[codeforces 547 E][51nod1440]迈克打电话

本文介绍了一种使用后缀数组(SA)解决字符串匹配问题的方法,通过构建SA并结合主席树来高效处理批量查询,实现了在给定字符串集合中查找特定子串出现频率的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

给出n个字符串,这n个字符串的总长为L,定义函数call(i,j)表示第j个字符串在第i个字符串中出现的次数。
给出q次询问,每次询问给出l,r,k,求ri=lcall(i,k)
n,l<=2*1e5,q<=5*1e5

Solution

蒟蒻不会fail树做法QwQ
只会最朴素的SA啊_ (:з」∠) _
首先把所有串接在一起建SA
那么对于每个串k,它作为子串的串在SA中一定是一段区间。
那么我们可以用二分找出这个区间,判断条件就是lcp是否大于串长
接下来用主席树维护这个区间中有多少个在[l,r]内的串。
这里写图片描述
卡常卡的欲仙欲死FAQ
18次提交还好51nod不算正确率23333
辣鸡卡常nod

Code

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

inline int read() {
    char ch;
    for(ch=getchar();ch<'0'||ch>'9';ch=getchar());
    int x=ch-'0';
    for(ch=getchar();ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
    return x;
}
inline void write(int x) {
    if (!x) {puts("0");return;}
    char ch[20];int tot=0;
    for(;x;x/=10) ch[++tot]=x%10+'0';
    fd(i,tot,1) putchar(ch[i]);
    puts("");
}

#define min(x,y) (x<y?x:y)
#define max(x,y) (x<y?y:x)
const int N=4*1e5+5;

int L,w[N],x[N],y[N],sa[N],rank[N],height[N];
char s[N];
inline void tsort() {
    int mx=0;
    fo(i,1,L) w[x[y[i]]]++,mx=max(mx,x[y[i]]);
    fo(i,1,mx) w[i]=w[i]+w[i-1];
    fd(i,L,1) sa[w[x[y[i]]]--]=y[i];
    fo(i,1,mx) w[i]=0;
}
inline void get_sa() {
    tsort();
    for(int j=1;j<=L;j<<=1) {
        int k=0;
        fo(i,L-j+1,L) y[++k]=i;
        fo(i,1,L) if (sa[i]>j) y[++k]=sa[i]-j;
        tsort();
        fo(i,1,L) y[i]=x[i],x[i]=0;
        x[sa[1]]=k=1;
        fo(i,2,L) {
            if (y[sa[i]]!=y[sa[i-1]]||y[sa[i]+j]!=y[sa[i-1]+j]) k++;
            x[sa[i]]=k;
        }
        if (k==L) break;
    }
    fo(i,1,L) rank[sa[i]]=i;
}
inline void get_height() {
    int k=0;
    fo(i,1,L) {
        if (k) k--;
        int j=sa[rank[i]-1];
        while (i+k<=L&&j+k<=L&&s[i+k]==s[j+k]) k++;
        height[rank[i]]=k; 
    }
}

int tot,ql,qr,k,sum,root[N],le[N<<4],ri[N<<4],tr[N<<4];
inline void insert(int &v,int l,int r,int x) {
    ++tot;
    le[tot]=le[v];ri[tot]=ri[v];
    tr[tot]=tr[v]+1;
    v=tot;
    if (l==r) return;
    int m=l+r>>1;
    if (x<=m) insert(le[v],l,m,x);
    else insert(ri[v],m+1,r,x);
}
inline void query(int v,int l,int r) {
    if (!v) return;
    if (ql<=l&&r<=qr) {sum=sum+tr[v];return;}
    int m=l+r>>1;
    if (m>=ql) query(le[v],l,m);
    if (m<qr) query(ri[v],m+1,r);
}

int n,q,len[N],pos[N],id[N];
char st[N];
inline void init() {
    n=read();q=read();
    fo(i,1,n) {
        char ch;pos[i]=++L;len[i]=1;
        for(ch=getchar();ch<'a'||ch>'z';ch=getchar());
        s[L]=ch;id[L]=i;
        for(ch=getchar();ch>='a'&&ch<='z';ch=getchar()) 
            ++len[i],id[++L]=i,s[L]=ch;
        s[++L]='a'+26;
    }
    fo(i,1,L) x[i]=s[i]-'a'+1;
    fo(i,1,L) y[i]=i;
    get_sa();get_height();
}

int f[19][N],two[19],lg[N];
inline int lcp(int x,int y) {
    if (x==y) return len[id[sa[x]]];
    x++;int z=lg[y-x+1];
    return min(f[z][x],f[z][y-two[z]+1]);
}
inline void prepare() {
    two[0]=1;fo(i,1,18) two[i]=two[i-1]<<1;
    fo(i,2,L) f[0][i]=height[i];
    fo(i,1,L) lg[i]=log(i)/log(2);
    fo(j,1,18)
        fo(i,2,L-two[j]+1)
            f[j][i]=min(f[j-1][i],f[j-1][i+two[j-1]]);
    fo(i,1,L) {
        root[i]=root[i-1];
        if (id[sa[i]]) insert(root[i],1,n,id[sa[i]]);
    }
}

int lx[N],rx[N];
inline int find_l(int l,int r,int k) {
    int now=r;
    while (l<r) {
        int mid=l+r>>1;
        if (lcp(mid,now)>=k) r=mid;
        else l=mid+1;
    }
    return l;
}
inline int find_r(int l,int r,int k) {
    int now=l;
    while (l<r) {
        int mid=l+r>>1;
        if (lcp(now,mid)>=k) l=mid+1;
        else r=mid;
    }
    if (lcp(now,l)<k) l--;
    return l;
}
inline void solve() {
    fo(i,1,n) {
        lx[i]=find_l(1,rank[pos[i]],len[i]);
        rx[i]=find_r(rank[pos[i]],L+1,len[i]);
    }
    for(;q;q--) {
        ql=read();qr=read();k=read();
        int lpos=lx[k],rpos=rx[k];
        int ans=0;
        sum=0;query(root[rpos],1,n);ans=ans+sum;
        sum=0;query(root[lpos-1],1,n);ans=ans-sum;
        write(ans);
    }
}

int main() {
    init();
    prepare();
    solve();
    return 0;
}
### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值