题意

给定一个长度为\(n\)的母串

\(q\)组询问

这个串可以旋转(就是把最后一位丢到最前面这样子)

问这个串以及其旋转的串在给定的串中出现了多少次

Sol

旋转就把它复制一遍接在后面

然后就在\(sam\)上匹配

跳\(parent\)树的父亲到最后一个长度大于等于询问串长

然后统计\(size\)

防止重复算,就标记一下这个点是否算过

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll; IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
} const int maxn(2e6 + 5); int len[maxn], trans[26][maxn], fa[maxn], tot = 1, last = 1;
int n, q, id[maxn], t[maxn], size[maxn], vis[maxn];
char s[maxn]; IL void Extend(RG int c){
RG int p = last, np = ++tot;
len[last = np] = len[p] + 1, size[np] = 1;
while(p && !trans[c][p]) trans[c][p] = np, p = fa[p];
if(!p) fa[np] = 1;
else{
RG int q = trans[c][p];
if(len[q] == len[p] + 1) fa[np] = q;
else{
RG int nq = ++tot;
fa[nq] = fa[q], len[nq] = len[p] + 1;
for(RG int i = 0; i < 26; ++i) trans[i][nq] = trans[i][q];
fa[np] = fa[q] = nq;
while(p && trans[c][p] == q) trans[c][p] = nq, p = fa[p];
}
}
} int main(){
scanf(" %s", s), n = strlen(s);
for(RG int i = 0; i < n; ++i) Extend(s[i] - 'a');
for(RG int i = 1; i <= tot; ++i) ++t[len[i]];
for(RG int i = 1; i <= tot; ++i) t[i] += t[i - 1];
for(RG int i = 1; i <= tot; ++i) id[t[len[i]]--] = i;
for(RG int i = tot; i; --i) size[fa[id[i]]] += size[id[i]];
q = Input();
for(RG int i = 1, l, tl; i <= q; ++i){
scanf(" %s", s + 1), l = strlen(s + 1);
for(RG int j = 1; j < l; ++j) s[j + l] = s[j];
RG ll ans = 0; tl = l + l;
for(RG int j = 1, nw = 1, cnt = 0; j < tl; ++j){
while(nw && !trans[s[j] - 'a'][nw]) nw = fa[nw], cnt = len[nw];
if(!nw) nw = 1, cnt = 0;
else ++cnt, nw = trans[s[j] - 'a'][nw];
while(nw && len[fa[nw]] >= l) nw = fa[nw], cnt = len[nw];
if(cnt >= l && vis[nw] != i) ans += size[nw], vis[nw] = i;
}
printf("%lld\n", ans);
}
return 0;
}

CF235C Cyclical Quest的更多相关文章

  1. CF235C Cyclical Quest(SAM)

    /* 统计串的出现次数显然可以在自动机上匹配出来即可 但是每次都挨个匹配的话会时间爆炸 那么考虑我们把串复制一份, 然后一起在后缀自动机上跑, 当我们匹配长度大于该串长度的时候强行失配即可 可能会有旋 ...

  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. CF235C 【Cyclical Quest】

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

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

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

  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. CF 235C. Cyclical Quest [后缀自动机]

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

随机推荐

  1. 1.HTML练习(二)

    一.表格练习: 1.<table>标签:声明一个表格,它的常用属性如下: border属性             定义表格的边框,设置值是数值 cellpadding属性     定义单 ...

  2. 40.oracle事务

    一.事务特性 事务必须具备以下四个特性,简称ACID属性 原子性(Atomicity):事务是一个完整的操作.事务的各步操作是不可分割的(原子的):要么都执行,要么都不执行场景:银行转账 A-100 ...

  3. js 封装trim()方法,去掉空格

    <script> //定义一个对象 - 名字是$ var $$ = function() {}; //原型 $$.prototype = { $id:function(id) { retu ...

  4. (Lua) C++ 寫函式,Lua 呼叫使用

    C++ 裡頭寫一個 Function 但是透過 Lua 提供input參數並且回傳結果給Lua 輸出的辦法. Lua 檔案 , , ) print("ave : ", ave, & ...

  5. jQuery 获取元素当前位置offset()与position()

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  6. curl 命令大全

    post json curl -H "Content-Type: application/json" -X POST --data '{"userID":100 ...

  7. Sublime 必知必会(持续更新)

    1.格式化代码 Edit - Line - Reindent(中文路径则是:编辑 - 行 - 再次缩进) 2.分屏显示 view-layout-Columns:2(中文路径则是:查看 - 布局 - 列 ...

  8. DotNetCore跨平台~autofac属性的自动注入

    回到目录 在使用autofac这个DI工具时,我们一般使用的是构造方法注入,而dotnetcore的标准框架里也集成了构造方法注入,而属性注入在一些场景下,表现的更为灵活,像java的spring框架 ...

  9. JavaScript设计模式-4.继承和聚合

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. Ubuntu系统Java开发环境的搭建

    操作系统:Linux x64 / Ubuntu 14.04 Java JDK版本:jdk-8u65-linux-x64.tar.gz 声明:转载请注明出处及本文链接 本文原文地址:Ubuntu系统Ja ...