Codeforces Round #146 (Div. 1) C - Cyclical Quest 后缀自动机+最小循环节
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std; const int N = 2e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, q, nx[N];
char s[N]; void getNext(char *s, int n) {
int k = ;
for(int i = ; i < n; i++) {
while(k && s[k] != s[i]) k = nx[k-];
if(s[k] == s[i]) k++;
nx[i] = k;
}
} struct SuffixAutomaton {
int last, cur, cnt, ch[N<<][], id[N<<], fa[N<<], dis[N<<], sz[N<<], c[N];
SuffixAutomaton() {cur = cnt = ;}
void init() {
for(int i = ; i <= cnt; i++) {
memset(ch[i], , sizeof(ch[i]));
sz[i] = c[i] = dis[i] = fa[i] = ;
}
cur = cnt = ;
}
int extend(int p, int c) {
cur = ++cnt; dis[cur] = dis[p]+;
for(; p && !ch[p][c]; p = fa[p]) ch[p][c] = cur;
if(!p) fa[cur] = ;
else {
int q = ch[p][c];
if(dis[q] == dis[p]+) fa[cur] = q;
else {
int nt = ++cnt; dis[nt] = dis[p]+;
memcpy(ch[nt], ch[q], sizeof(ch[q]));
fa[nt] = fa[q]; fa[q] = fa[cur] = nt;
for(; ch[p][c]==q; p=fa[p]) ch[p][c] = nt;
}
}
sz[cur] = ;
return cur;
}
void getSize(int n) {
for(int i = ; i <= cnt; i++) c[dis[i]]++;
for(int i = ; i <= n; i++) c[i] += c[i-];
for(int i = cnt; i >= ; i--) id[c[dis[i]]--] = i;
for(int i = cnt; i >= ; i--) sz[fa[id[i]]] += sz[id[i]];
} void solve() {
scanf("%s", s + );
n = strlen(s + );
for(int i = , last = ; i <= n; i++)
last = extend(last, s[i]-'a');
getSize(n);
scanf("%d", &q);
while(q--) {
scanf("%s", s + );
n = strlen(s + );
int tar = n;
getNext(s + , n);
int len = n%(n-nx[n-]) ? n : (n-nx[n-]);
for(int i = ; i < len; i++) s[++n] = s[i];
len = ;
LL ans = ;
for(int i = , p = ; i <= n; i++) {
while(p!= && !ch[p][s[i]-'a']) p = fa[p];
if(ch[p][s[i]-'a']) {
len = min(dis[p], len) + ;
p = ch[p][s[i]-'a'];
}
else len = ;
if(len >= tar) {
int u = p;
while(u != && dis[fa[u]] >= tar) u = fa[u];
if(u != ) ans += sz[u];
}
}
printf("%lld\n", ans);
}
}
} sam; int main() {
sam.solve();
return ;
} /*
*/
Codeforces Round #146 (Div. 1) C - Cyclical Quest 后缀自动机+最小循环节的更多相关文章
- Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)
http://codeforces.com/problemset/problem/182/D 题意:如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S ...
- codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...
- Codeforces Round #146 (Div. 2)
A. Boy or Girl 模拟题意. B. Easy Number Challenge 筛素数,预处理出\(d_i\). 三重循环枚举. C. LCM Challenge 打表找规律. 若\(n\ ...
- Codeforces 235C Cyclical Quest - 后缀自动机
Some days ago, WJMZBMR learned how to answer the query "how many times does a string x occur in ...
- 【Codeforces235C】Cyclical Quest 后缀自动机
C. Cyclical Quest time limit per test:3 seconds memory limit per test:512 megabytes input:standard i ...
- Codeforces Round #146 (Div. 1) B. Let's Play Osu! dp
B. Let's Play Osu! 题目连接: http://www.codeforces.com/contest/235/problem/B Description You're playing ...
- Codeforces Round #146 (Div. 1) A. LCM Challenge 水题
A. LCM Challenge 题目连接: http://www.codeforces.com/contest/235/problem/A Description Some days ago, I ...
- Codeforces Round #248 (Div. 1) D - Nanami's Power Plant 最小割
D - Nanami's Power Plant 思路:类似与bzoj切糕那道题的模型.. #include<bits/stdc++.h> #define LL long long #de ...
- CF 235C. Cyclical Quest [后缀自动机]
题意:给一个主串和多个询问串,求询问串的所有样子不同的周期同构出现次数和 没有周期同构很简单就是询问串出现次数,|Right| 有了周期同构,就是所有循环,把询问串复制一遍贴到后面啊!思想和POJ15 ...
随机推荐
- windows安装filebeat服务报错
cmd进入filebeat目录下 用以下命令执行: PowerShell.exe -ExecutionPolicy UnRestricted -File .\install-service-fil ...
- 新建 Vue项目 使用iView报错解决
错误如下: error in ./src/index.less Module build failed: // https://github.com/ant-design/ant-motion/iss ...
- Nginx location 配置用法及正则例子
Nginx location 配置语法 1. location [ = | ~ | ~* | ^~ ] uri { ... } 2. location @name { ... } ...
- [USACO07NOV]挤奶的时间Milking Time
https://daniu.luogu.org/problemnew/show/2889 按右端点从小到大排序后DP dp[i] 到第i个时间段的最大产奶量 不能按左端点排序,第i段由第j段更新时,第 ...
- python 分布式进程体验
抽了点时间体验了一把python 分布式进程,有点像分布式计算的意思,不过我现在还没有这个需求,先把简单体验的脚本发出来,供路过的各位高手指教 注:需要先下载multiprocessing 的pyth ...
- R8—批量生成文件夹,批量读取文件夹名称+R文件管理系统操作函数
一. 批量生成文件夹,批量读取文件夹名称 今日,工作中遇到这样一个问题:boss给我们提供了200多家公司的ID代码(如6007.7920等),需要根据这些ID号去搜索下载新闻,从而将下载到的新闻存到 ...
- java后台调用http请求
1:代码 @Value("${sms.username}") 可以将sms.properties配置文件中的值注入到username //这种方式是将sms.properti ...
- HDU 4370 0 or 1 (最短路)
[题目链接](http://acm.hdu.edu.cn/showproblem.ph Problem Description Given a n/n matrix Cij (1<=i,j< ...
- Oracle新建数据库,并导入dmp文件
1:安装Oracle及新建数据库 Oracle 11g安装图解 http://www.cnblogs.com/qianyaoyuan/archive/2013/05/05/3060471.html h ...
- 20155303 2016-2017-2 《Java程序设计》第三周学习总结
20155303 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 第四章 学会如何查询Java API文件对于Java的学习很有帮助,可以了解到如何使用各种方 ...