题目大意

给定一个模板串, 再给出\(n\)个询问, 询问每一个串的循环串总共在原串中出现了多少次.

循环串: 比如说有\(str[] = \{ABCD\}\), 则其循环串有\(\{ABCD\}, \{BCDA\}, \{CDAB\}, \{DABC\}\), 共\(len\)个.

题解

把每一个串复制一遍放在原串后面: \(\{ABCD\} \to \{ABCDABC\}\), 放入原串的后缀自动机中匹配. 在匹配时, 假如下一位无法匹配, 则跳suffix link; 假如即使跳了suffix link, 最大长度\(len(suffix)\)仍然大于等于原串长度, 则也跳suffix link(相当于砍掉多余的部分).

放入SAM前要先跑一次KMP去循环节.

#include <cstdio>
#include <cstring>
#include <vector> const int LEN = (int)1e6; struct suffixAutomaton
{
struct state
{
state *suc[26], *pre;
int len;
int sz, tg;
std::vector<state*> bck; inline state()
{
for(int i = 0; i < 26; ++ i)
suc[i] = NULL;
pre = NULL;
sz = 1;
bck.clear();
tg = 0;
}
}; state *rt, *lst; inline void insert(int c)
{
state *u = new state;
u->len = lst->len + 1;
for(; lst != NULL && lst->suc[c] == NULL; lst->suc[c] = u, lst = lst->pre);
if(lst == NULL)
u->pre = rt;
else
{
state *p = lst->suc[c];
if(p->len == lst->len + 1)
u->pre = p;
else
{
state *q = new state;
*q = *p;
q->len = lst->len + 1, q->sz = 0;
p->pre = u->pre = q;
for(; lst != NULL && lst->suc[c] == p; lst->suc[c] = q, lst = lst->pre);
}
}
lst = u;
} void DFS(state *u)
{
u->tg = 1;
if(u->pre != NULL)
u->pre->bck.push_back(u);
for(int i = 0; i < 26; ++ i)
if(u->suc[i] != NULL && ! u->suc[i]->tg)
DFS(u->suc[i]);
} void get(state *u)
{
for(std::vector<state*>::iterator p = u->bck.begin(); p != u->bck.end(); ++ p)
get(*p), u->sz += (*p)->sz;
} inline void build(char *str, int len)
{
lst = rt = new state;
rt->len = 0;
for(int i = 0; i < len; ++ i)
insert(str[i] - 'a');
DFS(rt);
get(rt);
} inline int match(char *str, int len, int cir)
{
state *u = rt;
int cur = 0;
long long ans = 0;
for(int i = 0; i < len + cir - 1; ++ i)
{
for(; u != rt && u->suc[str[i] - 'a'] == NULL; cur = u->pre->len, u = u->pre);
if(u->suc[str[i] - 'a'] != NULL)
u = u->suc[str[i] - 'a'], ++ cur;
for(; u != rt && u->pre->len >= len; cur = u->pre->len, u = u->pre);
if(cur >= len)
ans += u->sz;
}
return ans;
}
}SAM; int main()
{
#ifndef ONLINE_JUDGE
freopen("CF235C.in", "r", stdin);
#endif
static char str[LEN];
scanf("%s", str);
int len = strlen(str);
SAM.build(str, len);
int n;
scanf("%d\n", &n);
for(int i = 0; i < n; ++ i)
{
static char str[LEN << 1];
scanf("%s", str);
int len = strlen(str);
static int nxt[LEN];
nxt[0] = -1;
int p = nxt[0];
for(int i = 1; i < len; ++ i)
{
for(; ~ p && str[i] ^ str[p + 1]; p = nxt[p]);
nxt[i] = str[i] == str[p + 1] ? ++ p : p;
}
int cir = len % (len - nxt[len - 1] - 1) == 0 ? len - nxt[len - 1] - 1 : len;
for(int i = 0; i < cir; ++ i)
str[i + len] = str[i];
printf("%d\n", SAM.match(str, len, cir));
}
}

Codeforces 235 C的更多相关文章

  1. [codeforces 235]A. LCM Challenge

    [codeforces 235]A. LCM Challenge 试题描述 Some days ago, I learned the concept of LCM (least common mult ...

  2. codeforces 235 div2 C Team

    题目:http://codeforces.com/contest/401/problem/C 题意:n个0,m个1,求没有00或111的情况. 这么简单的题..... 做题的时候脑残了...,今天,贴 ...

  3. codeforces 235 B. Let's Play Osu!

    You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. F ...

  4. codeforces 235 div2 B. Sereja and Contests

    Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good ...

  5. codeforces 235 div2 A. Vanya and Cards

    Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer ...

  6. Codeforces 235 E Number Challenge

    Discription Let's denote d(n) as the number of divisors of a positive integer n. You are given three ...

  7. codeforces 235 B lets play osu!

    cf235B 一道有意思的题.(据说是美少女(伪)计算机科学家出的,hh) 根据题目要求,就是求ni^2的和. 而n^2=n*(n-1)+n; n*(n-1)=C(n,2)*2: 所以∑ai^2=∑a ...

  8. [ BZOJ 4318 & 3450 / CodeForces 235 B ] OSU!

    \(\\\) \(Description\) 一共进行\(N\)次操作,生成一个长度为\(N\)的\(01\)序列,成功对应\(1\),失败对应\(0\),已知每一次操作的成功率\(p_i\). 在这 ...

  9. Codeforces Round #235 (Div. 2) D. Roman and Numbers 状压dp+数位dp

    题目链接: http://codeforces.com/problemset/problem/401/D D. Roman and Numbers time limit per test4 secon ...

随机推荐

  1. LeetCode(152) Maximum Product Subarray

    题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...

  2. win7创建共享给windows和linux机器

    win7创建共享给windows和linux机器   1 防火墙 允许网络共享 由nod32负责防火墙设置 2 修改主机名 3 修改工作组和域 4 设置共享用户和权限 5 查看本机所有共享

  3. HBase0.94.2-cdh4.2.0需求评估测试报告1.0之四

    第二组:文件存储读过程记录 第一组:一个列,四个分区,随机ID 测试列和分区 测试程序或命令 导入文件大小(Mb) 导入文件个数(个) 是否触发flush事件(布尔) 是否触发compact事件(布尔 ...

  4. cf963b Destruction of a Tree

    越靠近叶子越优先删掉 #include <iostream> #include <vector> #include <cstdio> using namespace ...

  5. 04_ThreadLocal整合事务操作

    文章导读: 本文主要讲解了如何在没有框架情况下如何解决Dao的事务问题, 重点理解Connection存放到WeakReference中为什么垃圾回收的时候Connection不回收 视频与源码下载: ...

  6. Spring core resourc层结构体系及JDK与Spring对classpath中资源的获取方式及结果对比

    1. Spring core resourc层结构体系 1.1. Resource相关结构体系 1.2. ResourceLoader相关体系 2. JDK与Spring对classpath中资源的获 ...

  7. verilog 实现中值滤波

    图像信号在形成.传输和记录的过程中,由于成像系统.传输介质.工作环境和记录设备等的固有缺陷,不可避免地产生各种类型的噪声,降低了图像的质量,进而影响后续处理(如边缘检测.图像分割.特征提取.模式识别等 ...

  8. javascript学习笔记 - 引用类型 Array

    二 Array 1.可以通过length属性删除或创建新的数组项 arr = [1,2,3]; arr.length = 4;//增加 [1,2,3,undefined] arr.length = 2 ...

  9. 【P2387】魔法森林(SPFA非正解)

    题目链接 不会LCTqwq,看题解似乎SPFA也可以. 把边按a排序,从小到大每加一条边就以b为距离跑一遍SPFA,类似于Kruskal的想法吧…… 貌似是个暴力 (luoguLCT模块的题我都快通过 ...

  10. [luoguP2770] 航空路线问题(最小费用最大流)

    传送门 模型 求最长两条不相交路径,用最大费用最大流解决. 实现 为了限制经过次数,将每个点i拆成xi,yi. 1.从xi向yi连一条容量为1,费用为1的有向边(1<i<N), 2.从x1 ...