Cyclical Quest

\[Time Limit: 3000 ms\quad Memory Limit: 524288 kB
\]

题意

给出一个字符串为 \(s\) 串,接下来 \(T\) 个查询,枚举给出一个 \(t\) 串,求出 \(t\) 的所有循环子串在 \(s\) 串中出现的次数。

思路

对于每个查询串,因为要所有循环的子串,所以可以先把 \(t\) 串在复制一份到末尾,然后去求 \(LCS\)。

  • 如果在 \(p\) 节点查询到的 \(LCS==tlen\),那么 \(p\) 表示的子串就包含一种满足条件的循环 \(t\) 串。
  • 如果 \(LCS>tlen\),那就说明 \(p\) 包含了一种循环 \(t\) 串,但是匹配长度超过了循环 \(t\) 串,那么这个串的贡献一定在其 \(father\) 上,所以我们令一个临时变量 \(tmp\) 往 \(father\) 跳去寻找这个串的贡献。
  • 最后只要计算出每个节点包含的子串的出现次数,然后把这些满足条件的值加起来就可以了。

这里我们求 \(LCS\) 时直接用中间变量 \(res\)而不用往\(father\) 更新的。比如我现在在节点 \(u\),且 \(father[p]=u\),那么 \(p\) 在 \(u\) 的 \(LCS\) 就算更大,\(u\) 往其 \(father\) 的更新过程其实和 \(p\) 往 \(father\) 的更新过程是一样的,最后会停留在同一个节点,而一个节点的贡献只要算一次,所以 \(u\) 其实可以不往上更新。

还有这题的查询有 \(1e5\),而总的查询长度才 \(1e6\),所以可能出现查询多而每个串短的情况,所以对 \(vis\) 每次去 \(memset\) 是会 \(TLE\) 的...怀疑人生

#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e6 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std; int n, m;
int cas, tol, T; struct Sam {
int node[maxn<<1][27], step[maxn<<1], fa[maxn<<1];
int dp[maxn<<1], tax[maxn<<1], gid[maxn<<1];
int vis[maxn<<1];
int last, sz;
int newnode() {
mes(node[++sz], 0);
dp[sz] = step[sz] = fa[sz] = 0;
return sz;
}
void init() {
sz = 0;
last = newnode();
}
void insert(int k) {
int p = last, np = last = newnode();
dp[np] = 1;
step[np] = step[p]+1;
for(; p&&!node[p][k]; p=fa[p])
node[p][k] = np;
if(p==0) {
fa[np] = 1;
} else {
int q = node[p][k];
if(step[q] == step[p]+1) {
fa[np] = q;
} else {
int nq = newnode();
memcpy(node[nq], node[q], sizeof(node[q]));
fa[nq] = fa[q];
step[nq] = step[p]+1;
fa[np] = fa[q] = nq;
for(; p&&node[p][k]==q; p=fa[p])
node[p][k] = nq;
}
}
}
void handle() {
for(int i=0; i<=sz; i++) tax[i] = 0;
for(int i=1; i<=sz; i++) tax[step[i]]++;
for(int i=1; i<=sz; i++) tax[i] += tax[i-1];
for(int i=1; i<=sz; i++) gid[tax[step[i]]--] = i;
for(int i=sz; i>=1; i--) {
int u = gid[i];
dp[fa[u]] += dp[u];
}
}
int solve(char *s, int len, int id) {
int p = 1, res = 0;
int ans = 0;
for(int i=1; i<=len+len; i++) {
int k = s[i]-'a'+1;
while(p && !node[p][k]) {
p = fa[p];
res = step[p];
}
if(p == 0) {
p = 1;
res = 0;
} else {
p = node[p][k];
res++;
if(res >= len) {
int tmp = p;
while(vis[tmp]!=id &&!(step[fa[tmp]]+1<=len && len<=step[tmp])) {
vis[tmp] = id;
tmp = fa[tmp];
}
if(vis[tmp] != id) {
vis[tmp] = id;
ans += dp[tmp];
}
}
}
}
return ans;
}
} sam;
char s[maxn], t[maxn<<1]; int main() {
scanf("%s", s+1);
int slen = strlen(s+1);
sam.init();
for(int i=1; i<=slen; i++) {
sam.insert(s[i]-'a'+1);
}
sam.handle();
scanf("%d", &T);
for(int tt=1; tt<=T; tt++) {
scanf("%s", t+1);
int tlen = strlen(t+1);
for(int i=1; i<=tlen; i++) {
t[i+tlen] = t[i];
}
t[tlen+tlen+1] = '\0';
int ans = sam.solve(t, tlen, tt);
printf("%d\n", ans);
}
return 0;
}

Cyclical Quest CodeForces - 235C (后缀自动机)的更多相关文章

  1. Cyclical Quest CodeForces - 235C 后缀自动机

    题意: 给出一个字符串,给出一些子串,问每个子串分别在母串中圆环匹配的次数, 圆环匹配的意思是将该子串拆成两段再首位交换相接的串和母串匹配,比 如aaab变成baaa,abaa,aaba再进行匹配. ...

  2. Match & Catch CodeForces - 427D 后缀自动机水题

    题意: 给出两个字符串a,b,求一个字符串,这个字符串是a和b的子串, 且只在a,b中出现一次,要求输出这个字符串的最小长度. 题解: 将a串放入后缀自动机中,然后记录一下每个节点对应的子串出现的次数 ...

  3. cf 235C 后缀自动机

    题目大意 给定字符串\(S\)与\(n<=10^5\)个串\(x_1,x_2...x_n\)(总长\(\le10^6\)) 对于每个\(x_i\),输出有多少个\(S\)的子串与\(x_i\)循 ...

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

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

  5. 【CodeForces - 235C】Cyclical Quest 【后缀自动机】

    题意 给出一个字符串s1和q个询问,每个询问给出一个字符串s2,问这个询问的字符串的所有不同的周期串在s1中出现的次数的和. 分析 对于s1建后缀自动机.对于询问的每个字符串s2,我们按照处理循环串的 ...

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

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

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

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

  8. Codeforces 235C. Cyclical Quest

    传送门 写的时候挺蛋疼的. 刚开始的时候思路没跑偏,无非就是建个SAM然后把串开两倍然后在SAM上跑完后统计贡献.但是卡在第二个样例上就是没考虑相同的情况. 然后开始乱搞,发现会出现相同串的只有可能是 ...

  9. Codeforces 452E Three Strings(后缀自动机)

    上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...

随机推荐

  1. 百度前端技术学院-task1.10源代码

    任务十的源代码,其实有github的,但就是不知道怎么弄,近期会学会的.在IE和firefox上检测运行良好. <!DOCTYPE html> <html lang="en ...

  2. pytest_全局变量的使用

    这里重新阐述下PageObject设计模式: PageObject设计模式是selenium自动化最成熟,最受欢迎的一种模式,这里用pytest同样适用 这里直接提供代码: 全局变量 conftest ...

  3. MethodInvoker委托,跨线程访问

    Invoke(new MethodInvoker(delegate { textBox1.Enabled = true; })); 上面是简单缩写,也可以写成 private void btnOK_C ...

  4. C# Dapper 的简单实用

    首先引入dapper  PM>Install-Package Dapper -Version 2.0.4 (可能会出现因版本问题而安装失败详情见官网:https://stackexchange. ...

  5. Linux中Crontab的使用

    一.安装依赖 yum install cronie 二.添加 Crontab crontab -e 三.查看crontab内容 crontab -l 其中常见的一些内容 例子: # 每月的最后1天 0 ...

  6. 关于springMVC中的路径问题

    相对路径中,我们最后想要的到的是绝对路径,而绝对路径=参照路径+相对路径: 相对路径往往都知道,只需要区分参照路径即可:对于前台和后台,参照路径不太相同: 什么是前台,后台路径: 前台路径: 出现在 ...

  7. html5单词

    < meta   charset = " UTF-8 " >         国内编码     (meta-标签用来描述一个HTML网页文档的属性  charset-字 ...

  8. Calendar类set方法中的坑

    最近写了一个支付宝微信对账报表,发现系统金额比支付宝微信的少好多,左查右查发现是追缴金额没统计到,再一查发现月结束日期为2019-09-31,9月咋会有31,为啥呢就追缴金额不行呢,因为其他类型用TI ...

  9. java实现SAP BO登录

    最近一个项目用到了SAP的businessObjects,需要进行二次开发,今天开发了登录接口,遇到了一些问题,进行了解决,现在分享一下. 1.依赖jar包的添加 bo登录需要用到一些jar包,具体在 ...

  10. js浏览器对象模型【BOM】(十三)

    一.时间定时器1.超时调用setTimeout(fun,time)    [返回一个唯一标识该超时调用的ID数值]参数:fun:要执行的函数time:设置第多少毫秒后执行fun函数 clearTime ...