/*
统计串的出现次数显然可以在自动机上匹配出来即可
但是每次都挨个匹配的话会时间爆炸 那么考虑我们把串复制一份, 然后一起在后缀自动机上跑, 当我们匹配长度大于该串长度的时候强行失配即可 可能会有旋转后相同的串所以开个数组判重 */ #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. bzoj 4349 最小树形图——朱刘算法

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4349. 学习博客:http://www.cnblogs.com/xzxl/p/7243466 ...

  2. Jmeter JDBC Request--测试数据库连接 拒绝解决方案

    有时会遇到回应信息如下: 1 Cannot create PoolableConnectionFactory (Access denied for user 'root'@'10.0.1.23' (u ...

  3. 倚天剑ss

    倚天剑ss https://my.potvpn.com/ https://trial.ssbit.win/

  4. Elasticsearch的数据导出和导入操作(elasticdump工具),以及删除指定type的数据(delete-by-query插件)

    Elasticseach目前作为查询搜索平台,的确非常实用方便.我们今天在这里要讨论的是如何做数据备份和type删除.我的ES的版本是2.4.1. ES的备份,可不像MySQL的mysqldump这么 ...

  5. PHP破解wifi密码(wifi万能钥匙的接口)

    新建wifi.php,复制粘贴 <?php $bssid = $_POST["bssid"] ; $ssid = $_POST["ssid"] ; if ...

  6. ML平台_小米深度学习平台的架构与实践

    (转载:http://www.36dsj.com/archives/85383)机器学习与人工智能,相信大家已经耳熟能详,随着大规模标记数据的积累.神经网络算法的成熟以及高性能通用GPU的推广,深度学 ...

  7. shaderFX一些小心得

    同事需要一个能让贴图流动起来的shader,于是花了两天时间搞了一个,在这里要十分感谢同事的建议和提醒. 参考资料:<Digital Tutors - Implementing a Flow M ...

  8. WPF Demo18 路由事件

    using System.Windows; namespace 路由事件2 { public class Student { ////声明并定义路由事件 //public static readonl ...

  9. 黄聪:C#“多线程线程间操作无效: 从不是创建控件的线程访问它。”,跨线程修改控件属性解决方案

    解决方案就是使用代理,在代理中调用主线程的方法来控制控件 /// <summary> /// 声明代理 /// </summary> delegate void SetText ...

  10. locust -基础框架

    # coding=utf-8from locust import HttpLocust, TaskSet, taskimport requests # 定义用户行为class UserBehavior ...