codeforces 666E Forensic Examination

E. Forensic Examination
time limit per test6 seconds
memory limit per test768 megabytes
inputstandard input
outputstandard output
The country of Reberland is the archenemy of Berland. Recently the authorities of Berland arrested a Reberlandian spy who tried to bring the leaflets intended for agitational propaganda to Berland illegally . The most leaflets contain substrings of the Absolutely Inadmissible Swearword and maybe even the whole word.

Berland legal system uses the difficult algorithm in order to determine the guilt of the spy. The main part of this algorithm is the following procedure.

All the m leaflets that are brought by the spy are numbered from 1 to m. After that it’s needed to get the answer to q queries of the following kind: “In which leaflet in the segment of numbers [l, r] the substring of the Absolutely Inadmissible Swearword [pl, pr] occurs more often?”.

The expert wants you to automate that procedure because this time texts of leaflets are too long. Help him!

Input
The first line contains the string s (1 ≤ |s| ≤ 5·105) — the Absolutely Inadmissible Swearword. The string s consists of only lowercase English letters.

The second line contains the only integer m (1 ≤ m ≤ 5·104) — the number of texts of leaflets for expertise.

Each of the next m lines contains the only string ti — the text of the i-th leaflet. The sum of lengths of all leaflet texts doesn’t exceed 5·104. The text of the leaflets consists of only lowercase English letters.

The next line contains integer q (1 ≤ q ≤ 5·105) — the number of queries for expertise.

Finally, each of the last q lines contains four integers l, r, pl, pr (1 ≤ l ≤ r ≤ m, 1 ≤ pl ≤ pr ≤ |s|), where |s| is the length of the Absolutely Inadmissible Swearword.

Output
Print q lines. The i-th of them should contain two integers — the number of the text with the most occurences and the number of occurences of the substring [pl, pr] of the string s. If there are several text numbers print the smallest one.

Examples
input
suffixtree
3
suffixtreesareawesome
cartesiantreeisworsethansegmenttree
nyeeheeheee
2
1 2 1 10
1 3 9 10
output
1 1
3 4


【分析】

和bzoj 4556 有着异曲同工的感觉…
反正都不会做是真的。
和上题思路类似的SAM+权值线段树


【代码】

#include<bits/stdc++.h>
#define ll long long
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int M=24000005;
const int mxn=1200005;
char s[mxn];
int a,b,c,d,num,size,ans;
int cnt,m,T,p,q,np,nq,Q,tot,len;
int ls[M],rs[M],mx[M],id[M],B[mxn],C[mxn];
int color[mxn],pos[mxn],step[mxn],pre[mxn][24],son[mxn][28],root[mxn];
inline void update(int x)
{
    if(mx[ls[x]]==mx[x] && id[ls[x]]<id[x]) id[x]=id[ls[x]];
    if(mx[ls[x]]>mx[x]) mx[x]=mx[ls[x]],id[x]=id[ls[x]];
    if(mx[rs[x]]>mx[x]) mx[x]=mx[rs[x]],id[x]=id[rs[x]];
}
inline void insert(int &x,int l,int r,int v)
{
    if(!x) x=(++size);
    if(l==r) {mx[x]++,id[x]=l;return;}
    int mid=l+r>>1;
    if(v<=mid) insert(ls[x],l,mid,v);
    else insert(rs[x],mid+1,r,v);
    update(x);
}
inline void find(int x,int l,int r,int L,int R)
{
    if(!x) return;
    if(L<=l && r<=R)
    {
        if(ans<mx[x]) ans=mx[x],num=id[x];
        else if(ans==mx[x] && num>id[x]) num=id[x];
        return;
    }
    int mid=l+r>>1;
    if(R<=mid) find(ls[x],l,mid,L,R);
    else if(L>mid) find(rs[x],mid+1,r,L,R);
    else find(ls[x],l,mid,L,mid),find(rs[x],mid+1,r,mid+1,R);
}
inline int merge(int x,int y,int l,int r)
{
    if(!x) return y;
    if(!y) return x;
    int z=(++size),mid=l+r>>1;
    if(l==r)
    {
        mx[z]=mx[x]+mx[y];
        id[z]=l;
        return z;
    }
    ls[z]=merge(ls[x],ls[y],l,mid);
    rs[z]=merge(rs[x],rs[y],mid+1,r);
    update(z);
    return z;
}
inline void sam(int w)
{
    int i,j,c;
    fo(i,1,len+1)
    {
        p=np;
        if(i==len+1) c=27;
        else c=s[i]-'a'+1;
        step[np=(++tot)]=step[p]+1;
        if(!w && i<=len) pos[i]=np;
        if(w && i<=len) insert(root[np],1,cnt,w);
        while(p && !son[p][c])
          son[p][c]=np,p=pre[p][0];
        if(!p) {pre[np][0]=1;continue;}
        q=son[p][c];
        if(step[q]==step[p]+1)
          pre[np][0]=q;
        else
        {
            step[nq=(++tot)]=step[p]+1;
            memcpy(son[nq],son[q],sizeof son[q]);
            pre[nq][0]=pre[q][0];
            pre[q][0]=pre[np][0]=nq;
            while(p && son[p][c]==q)
              son[p][c]=nq,p=pre[p][0];
        }
    }
}
inline void init()
{
    int i,j;
    fo(i,1,tot) B[step[i]]++;
    fo(i,1,tot) B[i]+=B[i-1];
    fo(i,1,tot) C[B[step[i]]--]=i;
    for(i=tot;i>=1;i--)
    {
        int x=C[i],fa=pre[x][0];
        root[fa]=merge(root[fa],root[x],1,cnt);
    }
    fo(j,1,22) fo(i,1,tot) pre[i][j]=pre[pre[i][j-1]][j-1];
}
int main()
{
//  freopen("rand.txt","r",stdin);
//  freopen("WA.txt","w",stdout);
    int i,j;
    scanf("%s",s+1);
    len=strlen(s+1);
    tot=np=1,sam(0);
    scanf("%d",&cnt);
    fo(i,1,cnt)
    {
        scanf("%s",s+1);
        len=strlen(s+1);
        sam(i);
    }
    init();
    scanf("%d",&Q);
    while(Q--)
    {
        scanf("%d%d%d%d",&c,&d,&a,&b);
        len=b-a+1,b=pos[b];
        for(j=22;j>=0;j--)
          if(step[pre[b][j]]>=len)
            b=pre[b][j];
        ans=0,num=0,find(root[b],1,cnt,c,d);
        if(!ans) printf("%d %d\n",c,0);
        else printf("%d %d\n",num,ans);
    }
    return 0;
}
/*
abaccda
3
cebacb
cdcadda
caabdde
1
3 3 4 4
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值