zoukankan      html  css  js  c++  java
  • Codeforces 235C. Cyclical Quest

    传送门

    大致题意是给定一个字符串S,再给Q个询问,每次询问字符串Xi的循环同构在S中出现了多少次。

    主要应该注意,若Xi串有循环节,那么对Xi进行匹配的时候会算重(即S中相同的子串多次被算入答案),解决方法是后缀自动机上打标记,若此节点没有被算过答案,则计算答案,否则跳过。

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<string>
    #include<cstring>
    #include<cmath>
    #include<ctime>
    #include<algorithm>
    #include<iomanip>
    #include<set>
    #include<map>
    #include<queue>
    using namespace std;
    #define mem1(i,j) memset(i,j,sizeof(i))
    #define mem2(i,j) memcpy(i,j,sizeof(i))
    #define LL long long
    #define up(i,j,n) for(int i=(j);i<=(n);i++)
    #define FILE "dealing"
    #define poi vec
    #define eps 1e-10
    #define db double 
    const int maxn=2010000,inf=1000000000,mod=1000000007;
    int read(){
    	int x=0,f=1,ch=getchar();
    	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0',ch=getchar();}
    	return f*x;
    }
    bool cmax(int& a,int b){return a<b?a=b,true:false;}
    bool cmin(int& a,int b){return a>b?a=b,true:false;}
    struct SAM{
    	int pre[maxn],c[maxn][26],step[maxn],sa[maxn],cou[maxn],b[maxn],val[maxn],cnt,now,Len;
    	SAM(){mem1(pre,0);mem1(c,0);mem1(step,0);mem1(b,0);cnt=now=1;}
    	int extend(int x){
    		int np,nq,q,p;
    		p=now;now=np=++cnt;step[np]=step[p]+1;val[np]++;
    		while(p&&!c[p][x])c[p][x]=np,p=pre[p];
    		if(!p)pre[np]=1;
    		else {
    			q=c[p][x];
    			if(step[q]==step[p]+1)pre[np]=q;
    			else {
    				step[nq=++cnt]=step[p]+1;
    				mem2(c[nq],c[q]);
    				pre[nq]=pre[q];
    				pre[q]=pre[np]=nq;
    				while(p&&c[p][x]==q)c[p][x]=nq,p=pre[p];
    			}
    		}
    	}
    	int getsort(){
    		up(i,1,cnt)cou[step[i]]++;
    		up(i,1,cnt)cou[i]+=cou[i-1];
    		for(int i=cnt;i>=1;i--)sa[cou[step[i]]--]=i;
    		for(int i=cnt;i>=1;i--)val[pre[sa[i]]]+=val[sa[i]];
    	}
    	int walkprepare(){now=1,Len=0;}
    	int walk(int x){
    		while(pre[now]&&!c[now][x])now=pre[now],Len=step[now];
    		if(!c[now][x])return 0;
    		Len++;now=c[now][x];return Len;
    	}
    	int build(char* s){
    		int n=strlen(s+1);
    		up(i,1,n)extend(s[i]-'a');
    		walkprepare();
    		getsort();
    	}
    }a;
    char s[maxn];
    int main(){
    	freopen(FILE".in","r",stdin);
    	freopen(FILE".out","w",stdout);
    	scanf("%s",s+1);
    	a.build(s);
    	int Q=read();
    	while(Q--){
    		scanf("%s",s+1);int n=strlen(s+1);
    		up(i,1,n)s[i+n]=s[i];int nn=(n<<1)-1;
    		LL ans=0;
    		a.walkprepare();
    		up(i,1,nn){
    			int m=a.walk(s[i]-'a');
    			int p=a.now;
    			while(a.step[a.pre[p]]>=n)p=a.pre[p],a.Len=min(a.step[p],a.Len);
    			if(a.Len>=n&&a.b[p]!=Q+1)ans+=a.val[p],a.b[p]=Q+1;
    		}
    		printf("%lld
    ",ans);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    云计算和SOA何时走到了一起?
    MVP
    Mvp
    Technology Radar of thoughtworks
    JSF
    我们要积极学习互联网的用户体验
    Gwt
    数字的字符串处理
    C语言字符串函数大全(转自百度百科)
    树状数组
  • 原文地址:https://www.cnblogs.com/chadinblog/p/6422355.html
Copyright © 2011-2022 走看看