/*
统计串的出现次数显然可以在自动机上匹配出来即可
但是每次都挨个匹配的话会时间爆炸 那么考虑我们把串复制一份, 然后一起在后缀自动机上跑, 当我们匹配长度大于该串长度的时候强行失配即可 可能会有旋转后相同的串所以开个数组判重 */ #include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#define ll long long
#define M 2000100
#define mmp make_pair
using namespace std;
int read()
{
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
char s[M];
int ch[M][26], fa[M], len[M], sz[M], tot[M], a[M], lst = 1, cnt = 1;
int vis[M]; void insert(int c)
{
int p = ++cnt, f = lst;
lst = p;
len[p] = len[f] + 1;
sz[p] = 1;
while(f && !ch[f][c]) ch[f][c] = p, f = fa[f];
if(!f)
{
fa[p] = 1;
}
else
{
int q = ch[f][c];
if(len[q] == len[f] + 1)
{
fa[p] = q;
}
else
{
int nq = ++cnt;
memcpy(ch[nq], ch[q], sizeof(ch[q]));
fa[nq] = fa[q];
len[nq] = len[f] + 1;
fa[p] = fa[q] = nq;
while(f && ch[f][c] == q) ch[f][c] = nq, f = fa[f];
}
}
} int main()
{
scanf("%s", s + 1);
int l = strlen(s + 1);
for(int i = 1; i <= l; i++) insert(s[i] - 'a');
for(int i = 1; i <= cnt; i++) tot[len[i]]++;
for(int i = 1; i <= cnt; i++) tot[i] += tot[i - 1];
for(int i = 1; i <= cnt; i++) a[tot[len[i]]--] = i;
for(int i = cnt; i >= 1; i--) sz[fa[a[i]]] += sz[a[i]];
int m = read();
for(int cor = 1; cor <= m; cor++)
{
scanf("%s", s + 1);
l = strlen(s + 1);
for(int i = 1; i <= l; i++) s[l + i] = s[i];
int now = 1, lenn = 0;
ll ans = 0;
for(int i = 1; i <= l * 2; i++)
{
int c = s[i] - 'a';
while(now && !ch[now][c]) now = fa[now], lenn = len[now];
if(!now) now = 1, lenn = 0;
else now = ch[now][c], lenn++;
while(now && len[fa[now]] >= l) now = fa[now], lenn = len[now]; //只能统计那些刚刚超过的节点
if(lenn >= l && vis[now] != cor) vis[now] = cor, ans += sz[now];
}
cout << ans << "\n";
}
return 0;
}

CF235C Cyclical Quest(SAM)的更多相关文章

  1. CF235C Cyclical Quest

    题意 给定一个长度为\(n\)的母串 \(q\)组询问 这个串可以旋转(就是把最后一位丢到最前面这样子) 问这个串以及其旋转的串在给定的串中出现了多少次 Sol 旋转就把它复制一遍接在后面 然后就在\ ...

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

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

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

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

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

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

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

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

  6. CF235C 【Cyclical Quest】

    厚颜无耻的发一篇可能是全网最劣解法 我们发现要求给定的串所有不同的循环同构出现的次数,可以直接暴力啊 因为一个长度为\(n\)的串,不同的循环同构次数显然是不会超过\(n\)的,所以我们可以直接对每一 ...

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

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

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

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

  9. Codeforces Round #146 (Div. 1) C - Cyclical Quest 后缀自动机+最小循环节

    #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...

随机推荐

  1. Spring技术内幕总结 - AOP概述

    AOP是Aspect-Oriented Programming(面向方面/切面编程)的简称.Aspect是一种新的模块化机制,用来描述分散在对象.类或函数中的横切关注点.分离关注点使解决特定领域问题的 ...

  2. 手动制作openstack windows镜像

    https://docs.openstack.org/image-guide/windows-image.html 这里以 windows 2008为例: 准备工作: 1准备好windows 2008 ...

  3. hadoop本地集群搭建

    网络配置 1.永久性配置CentOS网络 vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=y ...

  4. Device Identifier and Device DNA初识

    Device Identifier and Device DNA初识 信息来源 怎么样去用这个DNA:

  5. 进程池pool

    如果有多个进程,同一时间只能有限个给cpu运行 from multiprocessing import Process,Pool import time,os def bar(arg): print( ...

  6. SQL 行列转置

    ),),[Score] int) Insert Class union all union all union all union all union all union all union all ...

  7. Delphi调用DLL中的接口

    问题描述: 具体问题就是在隐式使用接口变量后,在FreeLibrary执行后,就会出现一个非法访址的错误. 这个错误的原因就是在FreeLibrary后,DLL以的代码均为不可用状态,而在代码执行完整 ...

  8. https 不检验证书

    System.Net.ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErro ...

  9. MFC系统自动生成的停靠窗格关掉后,如何重新显示?

    就是这几个x,关闭之后,再也显示不出来了: 原来,系统会默认记忆上次的状态,可以用函数来清除这个设置: 在MainFrame那个类中,CreateDocablePane之前,调用EnableLoadD ...

  10. [蓝桥杯]ALGO-92.算法训练_前缀表达式

    问题描述 编写一个程序,以字符串方式输入一个前缀表达式,然后计算它的值.输入格式为:“运算符 对象1 对象2”,其中,运算符为“+”(加法).“-”(减法).“*”(乘法)或“/”(除法),运算对象为 ...