传送门

写的时候挺蛋疼的。

刚开始的时候思路没跑偏,无非就是建个SAM然后把串开两倍然后在SAM上跑完后统计贡献。但是卡在第二个样例上就是没考虑相同的情况。

然后开始乱搞,发现会出现相同串的只有可能是由一个串无限拼接构成的串,于是上了个KMP来搞循环串..然后就没有然后了

基本上一直处于改了这个错误后又被另一个错误卡着的状态,后来没办法了,翻题解..

然后发现只需要在SAM的节点上开个标记就行了...

越学越傻啊...

至于为什么KMP会挂掉..求dalao评论指出

//Codeforces 235C
//by Cydiater
//2017.1.22
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n)	for(int i=j;i<=n;i++)
#define down(i,j,n)	for(int i=j;i>=n;i--)
#define cmax(a,b)	a=max(a,b)
#define cmin(a,b)	a=min(a,b)
const int MAXN=2e6+5;
const int oo=0x3f3f3f3f;
int rnk[MAXN],N,Right[MAXN],M,len,ans,flag[MAXN];
char s[MAXN];
struct SAM{
	int now,cnt,son[MAXN][26],step[MAXN],pre[MAXN],label[MAXN];
	SAM(){now=cnt=1;}
	void Extend(int nxt){
		int p=now,np=++cnt;now=np;
		step[np]=step[p]+1;Right[np]=1;
		for(;(!son[p][nxt])&&p;p=pre[p])son[p][nxt]=np;
		if(!p)pre[np]=1;
		else{
			int q=son[p][nxt],nq;
			if(step[q]==step[p]+1)pre[np]=q;
			else{
				step[(nq=++cnt)]=step[p]+1;
				memcpy(son[nq],son[q],sizeof(son[q]));
				pre[nq]=pre[q];
				pre[np]=pre[q]=nq;
				for(;son[p][nxt]==q;p=pre[p])son[p][nxt]=nq;
			}
		}
	}
	void Toposort(){
		up(i,1,cnt)label[step[i]]++;
		up(i,1,N)label[i]+=label[i-1];
		up(i,1,cnt)rnk[label[step[i]]--]=i;
		down(i,cnt,1){
			int node=rnk[i];
			Right[pre[node]]+=Right[node];
		}
	}
	void Go(){
		now=1;int Len=0;
		up(i,1,(len<<1)-1){
			int nxt=s[i]-'a';
			for(;now&&(!son[now][nxt]);now=pre[now],Len=step[now]);
			if(!now){now=1;Len=0;}
			if(son[now][nxt]){now=son[now][nxt];Len++;}
			for(;now&&step[pre[now]]>=len;now=pre[now],Len=step[now]);
			if(step[pre[now]]<len&&step[now]>=len&&Len>=len){
				if(flag[now]!=M+1){
					ans+=Right[now];
					flag[now]=M+1;
				}
			}
		}
	}
}sam;
namespace solution{
	void Prepare(){
		scanf("%s",s+1);
		N=strlen(s+1);
		up(i,1,N)sam.Extend(s[i]-'a');
		sam.Toposort();
	}
	void Solve(){
		cin>>M;
		while(M--){
			scanf("%s",s+1);
			len=strlen(s+1);
			up(i,1,len-1)s[i+len]=s[i];
			ans=0;
			sam.Go();
			printf("%d\n",ans);
		}
	}
}
int main(){
	//freopen("input.in","r",stdin);
	using namespace solution;
	Prepare();
	Solve();
	return 0;
}

Codeforces 235C. Cyclical Quest的更多相关文章

  1. Codeforces 235C Cyclical Quest - 后缀自动机

    Some days ago, WJMZBMR learned how to answer the query "how many times does a string x occur in ...

  2. CodeForces 235C Cyclical Quest(后缀自动机)

    [题目链接] http://codeforces.com/contest/235/problem/C [题目大意] 给出一个字符串,给出一些子串,问每个子串分别在母串中圆环匹配的次数,圆环匹配的意思是 ...

  3. Codeforces 235C Cyclical Quest 字符串 SAM KMP

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF235C.html 题目传送门 -  CF235C 题意 给定一个字符串 $s$ ,多组询问,每组询问的形式为 ...

  4. CF 235C. Cyclical Quest [后缀自动机]

    题意:给一个主串和多个询问串,求询问串的所有样子不同的周期同构出现次数和 没有周期同构很简单就是询问串出现次数,|Right| 有了周期同构,就是所有循环,把询问串复制一遍贴到后面啊!思想和POJ15 ...

  5. Cyclical Quest CodeForces - 235C (后缀自动机)

    Cyclical Quest \[ Time Limit: 3000 ms\quad Memory Limit: 524288 kB \] 题意 给出一个字符串为 \(s\) 串,接下来 \(T\) ...

  6. 【Codeforces235C】Cyclical Quest 后缀自动机

    C. Cyclical Quest time limit per test:3 seconds memory limit per test:512 megabytes input:standard i ...

  7. 【CF235C】Cyclical Quest(后缀自动机)

    [CF235C]Cyclical Quest(后缀自动机) 题面 洛谷 题解 大致翻译: 给定一个串 然后若干组询问 每次也给定一个串 这个串可以旋转(就是把最后一位丢到最前面这样子) 问这个串以及其 ...

  8. 【CodeForces - 235C】Cyclical Quest 【后缀自动机】

    题意 给出一个字符串s1和q个询问,每个询问给出一个字符串s2,问这个询问的字符串的所有不同的周期串在s1中出现的次数的和. 分析 对于s1建后缀自动机.对于询问的每个字符串s2,我们按照处理循环串的 ...

  9. Cyclical Quest CodeForces - 235C 后缀自动机

    题意: 给出一个字符串,给出一些子串,问每个子串分别在母串中圆环匹配的次数, 圆环匹配的意思是将该子串拆成两段再首位交换相接的串和母串匹配,比 如aaab变成baaa,abaa,aaba再进行匹配. ...

随机推荐

  1. ajax请求加全局loading , 个别特殊请求不显示loading

    项目中,请求开始前加载loading遮罩层,请求结束关闭遮罩,一般都会加在全局中,但有个别请求不需要加全局loading 的话,这时候就需要对这些请求进行配置 全局加loading: ; functi ...

  2. /usr/local/nginx/sbin/nginx -s reload 失败原因pid 进程记录和当前不符

    [root@a ~]# /usr/local/nginx/sbin/nginx -s reload;nginx: [alert] kill(18834, 1) failed (3: No such p ...

  3. Yii框架2.0的Gii

    Yii框架的Gii在我看来算是个快速创建器,当然对于学习来说意义不大,但对于已经懂得他的原理并用他开发的话,就是个快速开发的好工具. 他能快速的创建控制器,模块,crup,插件,Module. 打开g ...

  4. 汉诺塔IV---hdu2077

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2077 #include <stdio.h> #include <stdlib.h&g ...

  5. Ubutun使用记录——语系错误(转)

    add by zhj: 对原文有修改,原文是在创建用户时出现的问题,而我是在使用psql时出现的, 但问题是相同的. 原文:http://www.douban.com/note/362250557/ ...

  6. javascript 之 typeof 与 instanceof

    1.typeof:返回一个表达式的数据类型的字符串 返回结果为js的数据类型,包括number,boolean,string,object,undefined,function. var a = 1; ...

  7. HTTP来源地址

    HTTP来源地址(referer,或HTTP referer),是HTTP表头的一个字段,用来表示从哪儿链接到目前的网页,采用的格式是URL. 换句话说,借着HTTP来源地址,目前的网页可以检查访客从 ...

  8. sql server中使用xp_cmdshell

    关键词:sql server开启高级配置,使用Bat,cmdshell 1.sql server中使用xp_cmdshell --允许配置高级选项 GO RECONFIGURE GO . --开启xp ...

  9. java 字符串截取的方法

    1.split()+正则表达式来进行截取. 将正则传入split().返回的是一个字符串数组类型.不过通过这种方式截取会有很大的性能损耗,因为分析正则非常耗时. String str = " ...

  10. Nginx+tomcat配置负载均衡集群

    操作系统版本:Centos 6.4 Nginx版本:nginx-1.3.15.tar.gz wget http://nginx.org/download/nginx-1.5.9.tar.gz JDK版 ...