CF235C Cyclical Quest
题意
给定一个长度为\(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的更多相关文章
- CF235C Cyclical Quest(SAM)
/* 统计串的出现次数显然可以在自动机上匹配出来即可 但是每次都挨个匹配的话会时间爆炸 那么考虑我们把串复制一份, 然后一起在后缀自动机上跑, 当我们匹配长度大于该串长度的时候强行失配即可 可能会有旋 ...
- 【CF235C】Cyclical Quest(后缀自动机)
[CF235C]Cyclical Quest(后缀自动机) 题面 洛谷 题解 大致翻译: 给定一个串 然后若干组询问 每次也给定一个串 这个串可以旋转(就是把最后一位丢到最前面这样子) 问这个串以及其 ...
- 【Codeforces235C】Cyclical Quest 后缀自动机
C. Cyclical Quest time limit per test:3 seconds memory limit per test:512 megabytes input:standard i ...
- Cyclical Quest CodeForces - 235C (后缀自动机)
Cyclical Quest \[ Time Limit: 3000 ms\quad Memory Limit: 524288 kB \] 题意 给出一个字符串为 \(s\) 串,接下来 \(T\) ...
- CF235C 【Cyclical Quest】
厚颜无耻的发一篇可能是全网最劣解法 我们发现要求给定的串所有不同的循环同构出现的次数,可以直接暴力啊 因为一个长度为\(n\)的串,不同的循环同构次数显然是不会超过\(n\)的,所以我们可以直接对每一 ...
- Codeforces 235C Cyclical Quest 字符串 SAM KMP
原文链接https://www.cnblogs.com/zhouzhendong/p/CF235C.html 题目传送门 - CF235C 题意 给定一个字符串 $s$ ,多组询问,每组询问的形式为 ...
- Codeforces 235C Cyclical Quest - 后缀自动机
Some days ago, WJMZBMR learned how to answer the query "how many times does a string x occur in ...
- CodeForces 235C Cyclical Quest(后缀自动机)
[题目链接] http://codeforces.com/contest/235/problem/C [题目大意] 给出一个字符串,给出一些子串,问每个子串分别在母串中圆环匹配的次数,圆环匹配的意思是 ...
- CF 235C. Cyclical Quest [后缀自动机]
题意:给一个主串和多个询问串,求询问串的所有样子不同的周期同构出现次数和 没有周期同构很简单就是询问串出现次数,|Right| 有了周期同构,就是所有循环,把询问串复制一遍贴到后面啊!思想和POJ15 ...
随机推荐
- C# 并发队列ConcurrentQueue
测试函数 static async Task RunProgram() { var taskQueue = new ConcurrentQueue<CustomTask>(); var c ...
- CountDownLatch的简单实现
1. @Data public abstract class BaseLatch { private int limit; protected int running; BaseLatch(int l ...
- java.lang.Exception: The server rejected the connection: None of the protocols were accepted
solution for this is from comment for https://issues.jenkins-ci.org/browse/JENKINS-29616. The follow ...
- mono for android读书笔记之硬件编程(转)
本章将会介绍: 传感器的API 加速器编程,设备的方向,近场检测 网络编程 蓝牙编程 上述的技术的应用场景很多,比如: 1.检测当前的网络是否可用,并提醒用户,检测当前的网络类型,比如Wifi.3G. ...
- (转)搞个这样的APP要多久?心酸啊。
这是一个“如有雷同,纯属巧合”的故事,外加一些废话,大家请勿对号入座.开始了…… 我有些尴尬地拿着水杯,正对面坐着来访的王总,他是在别处打拼的人,这几年据说收获颇丰,见移动互联网如火如荼,自然也想着要 ...
- js Array vs [],以及是否为空的判断
两者基本相同,唯一不同点在于初始化: var a = [], // these are the same b = new Array(), // a and b are arrays with len ...
- strcpy和strncpy
在c语言中,对于简单变量,如int型.double型,直接使用赋值符号“=”,即可完成赋值,如 int a=10: int b: b=a: 即可完成用a给b赋值. 但是对于字符串,这样赋值是不准确的. ...
- 去掉img标签周围的空白
我们在页面布局的时候,明明已经去掉了所有标签的margin和padding,img标签周围依然会有空白,解决方法有以下几种: 1.给img标签设浮动: img{ float:left; } 2.将im ...
- ORACLE: private ,dao中util执行规范,nextval计数把通过nextval插入但已删除的列也统计在内向后计数
private DAO中的util.rs.sql都应该为private. 其中每个具体方法执行增删改查操作前打开数据库连接,操作完成后关闭数据库连接.操作要规范,不然易出错. nextval seq_ ...
- 数据存储之第三方FMDB优化
最近项目要用到数据库,采用的是第三方FMDB, 之前做C#时用过sqlHelper,自己就按着sqlHelper的思路封装了一下,封装的也比较简单,看到网上有一些根据FMDB封装的ORM框架,但基本都 ...