将S建后缀自动机,对于每个串复制两倍的长度(2L)在自动机上跑,

统计长度为L时,对应节点的出现次数

C. Cyclical Quest
time limit per test

3 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Some days ago, WJMZBMR learned how to answer the query "how many times does a string x occur in a string s" quickly by preprocessing the string s. But now he wants to make it harder.

So he wants to ask "how many consecutive substrings of s are cyclical isomorphic to a given string x". You are given string s and n stringsxi, for each string xi find, how many consecutive substrings of s are cyclical isomorphic to xi.

Two strings are called cyclical isomorphic if one can rotate one string to get the other one. 'Rotate' here means 'to take some consecutive chars (maybe none) from the beginning of a string and put them back at the end of the string in the same order'. For example, string "abcde" can be rotated to string "deabc". We can take characters "abc" from the beginning and put them at the end of "de".

Input

The first line contains a non-empty string s. The length of string s is not greater than 106 characters.

The second line contains an integer n (1 ≤ n ≤ 105) — the number of queries. Then n lines follow: the i-th line contains the string xi — the string for the i-th query. The total length of xi is less than or equal to 106 characters.

In this problem, strings only consist of lowercase English letters.

Output

For each query xi print a single integer that shows how many consecutive substrings of s are cyclical isomorphic to xi. Print the answers to the queries in the order they are given in the input.

Sample test(s)
input
baabaabaaa
5
a
ba
baa
aabaa
aaba

output
7
5
7
3
5

input
aabbaa
3
aa
aabb
abba

output
2
3
3

/* ***********************************************
Author        :CKboss
Created Time  :2015年06月16日 星期二 19时37分19秒
File Name     :CF235C.cpp
************************************************ */#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>using namespace std;/************SAM****************/const int CHAR=27,maxn=2000200;struct SAM_Node
{SAM_Node *fa,*next[CHAR];int len,id,pos;SAM_Node(){}SAM_Node(int _len){fa=0; len=_len;memset(next,0,sizeof(next));}
};SAM_Node SAM_node[maxn],*SAM_root,*SAM_last;
int SAM_size;SAM_Node *newSAM_Node(int len)
{SAM_node[SAM_size]=SAM_Node(len);SAM_node[SAM_size].id=SAM_size;return &SAM_node[SAM_size++];
}SAM_Node *newSAM_Node(SAM_Node *p)
{SAM_node[SAM_size]=*p;SAM_node[SAM_size].id=SAM_size;return &SAM_node[SAM_size++];
}void SAM_init()
{SAM_size=0;SAM_root=SAM_last=newSAM_Node(0);SAM_node[0].pos=0;
}void SAM_add(int x,int len)
{SAM_Node *p=SAM_last,*np=newSAM_Node(p->len+1);np->pos=len; SAM_last=np;for(;p&&!p->next[x];p=p->fa) p->next[x]=np;if(!p) { np->fa=SAM_root; return ; }SAM_Node *q=p->next[x];if(q->len==p->len+1) { np->fa=q; return ; }SAM_Node *nq=newSAM_Node(q);nq->len=p->len+1;q->fa=nq; np->fa=nq;for(;p&&p->next[x]==q;p=p->fa) p->next[x]=nq;
}/// !!!!!!!!!!!!! 统计每个节点出现的次数int c[maxn],num[maxn];
SAM_Node* top[maxn];void Count(char str[],int len)
{for(int i=0;i<SAM_size;i++) c[SAM_node[i].len]++;for(int i=1;i<=len;i++) c[i]+=c[i-1];for(int i=0;i<SAM_size;i++) top[--c[SAM_node[i].len]]=&SAM_node[i];SAM_Node *p=SAM_root;for(;p->len!=len;p=p->next[str[p->len]-'a']) num[p->id]=1; num[p->id]=1;for(int i=SAM_size-1;i>=0;i--){p=top[i];if(p->fa){SAM_Node *q=p->fa; num[q->id]+=num[p->id];}}
}/************SAM****************/char stmain[maxn],str[maxn];
int tn;
int vis[maxn];int READ_STR(char *str)
{int i=0;char ch;while(true){ch=getchar();if(ch=='\n') break;str[i++]=ch;}str[i]=0;return i;
}int main()
{//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);int len=READ_STR(stmain);SAM_init();for(int i=0;i<len;i++) SAM_add(stmain[i]-'a',i+1);Count(stmain,len);scanf("%d",&tn);getchar();int cas=0;while(tn--){int ans=0;cas++;int n=READ_STR(str);SAM_Node* cur=SAM_root;int nowlen=0;for(int i=0;i<2*n-1;i++){if(nowlen==n){nowlen--;if(nowlen<=cur->fa->len)cur=cur->fa;}int id=i;if(id>=n) id-=n;int to = str[id]-'a';while(cur!=0&&cur->next[to]==0){cur=cur->fa;if(cur!=0) nowlen=cur->len;}if(cur!=0&&cur->next[to]!=0){cur=cur->next[to];nowlen++;}else{cur=SAM_root;nowlen=0;}if(nowlen==n){if(vis[cur->id]!=cas){vis[cur->id]=cas;ans+=num[cur->id];}}}printf("%d\n",ans);}return 0;
}

Codeforces 235C. Cyclical Quest 后缀自动机相关推荐

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

    题目链接: https://codeforces.com/contest/235/problem/C 题解: 对大串建后缀自动机 对询问串复制拆环.这里一定要注意是复制一个循环节不是复制整个串!循环节 ...

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

    题意:给一个主串,再给出多个模式串,分别求主串中有多少个连续子串,与模式串循环同构. 题解:后缀自动机 因为要求循环同构,所以将模式串复制放到后面.(要么加终止符,要么传入长度) 先对主串建sam,然 ...

  3. Codeforces #235 C.Cyclical Quest(后缀自动机)

    传送门 题意:给定一个模式串和nnn个匹配串,询问原串有多少个子串和匹配串循环同构 考虑要求循环同构,于是先对SSS建出后缀自动机 把每次询问的XXX倍长在自动机上跑 如果当前匹配的长度已经超过原串长 ...

  4. Codeforces.700E.Cool Slogans(后缀自动机 线段树合并 DP)

    题目链接 \(Description\) 给定一个字符串\(s[1]\).一个字符串序列\(s[\ ]\)满足\(s[i]\)至少在\(s[i-1]\)中出现过两次(\(i\geq 2\)).求最大的 ...

  5. 后缀自动机(探索)Codeforces 427D

    想学后缀自动机的 弱鸡 表示真自闭啊 开局一道题,内容全靠水 :Codeforces 427D 有题目才能更好地学 算法 题意很简单 :给两个字符串,求最短公共子串 的长度 后缀自动机 模板很多 ,给 ...

  6. CodeForces - 427D Match Catch(后缀数组/广义后缀自动机)

    题目链接:点击查看 题目大意:给出两个字符串,求出两个字符串中的最短公共子串,且在每个字符串中只出现过一次 题目分析:因为这个公共子串只能在字符串中出现一次,考虑到用后缀数组,我们先将两个字符串通过特 ...

  7. Codeforces Round #364 (Div. 1) (差一个后缀自动机)

    B. Connecting Universities 大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值 单独考虑每条边$(u,v)$的贡献即可, ...

  8. Codeforces 235C

    Codeforces 235C 题目:给定一主串\(S\),\(n\)次询问,每次询问串\(t\)的所有循环移位串的出现的次数和 做法:建\(SAM\),对于询问串\(t\),将他复制一份放在后边,在 ...

  9. CF 316G3 Good Substrings——广义后缀自动机

    题目:http://codeforces.com/contest/316/problem/G3 对询问串和模式串一起建一个后缀自动机,做出在每个串上的 right 集合大小之后枚举自动机上的每个点看看 ...

  10. 【POJ1509】Glass Beads 【后缀自动机】

    题意 给出一个字符串,求它的最小表示法. 分析 这个题当然可以用最小表示法做啦!但是我是为了学后缀自动机鸭! 我们把这个字符串长度乘二,然后建SAM,然后在SAM上每次跑最小的那个字母,找出长度为n的 ...

最新文章

  1. “中药资源创新院士团队”诚聘结构、合成和计算生物学博士后5-10名
  2. MYSQL韩文显示正常一法
  3. python os.popen.readlines异常_python中执行sed命令操作源文件时出现错误
  4. 打印dataframe的前十行_小学生之十行Python解高思五星题(一)
  5. 处理数字_7_含NULL值的列的聚合
  6. python random库下载_Python---random库(随机数)
  7. 解决开发问题的思路与心态
  8. oopc——1.抽象
  9. PHP compact函数
  10. [RTMP协议]常用直播流地址
  11. 将第二台笔记本电脑用作带有Windows 10无线显示器的扩展显示器
  12. 函数式编程-Either函子篇
  13. 大数据分析技术应用领域有哪些
  14. APP用户界面设计六基本原则
  15. golang 枚举 iota
  16. Commvault发布横向扩展一体机 矛头对准Rubrik和Cohesity
  17. uniapp页面导出pdf
  18. 用C语言写一个 将从终端上接收到的8个一字节数据用3des加密之后再连接上随机生成的八个一字节的数据再发送给终端...
  19. 4.1 I/O流
  20. 【每日一个GitHub项目】GitHub中文排行榜

热门文章

  1. 基于JavaWeb聊天室设计与实现
  2. win7 install solution for intel SKL and BSW platform
  3. What is UTF-8?
  4. C. Inna and Dima
  5. 工行网银B2c第三方接口开发
  6. 小菜鸟szx的测试博文
  7. JS中各种width和height的区别
  8. 台式机显示屏作为笔记本显示屏的设置
  9. javascript汉字转拼音代码
  10. QT学习教程(全面)