题目大意

给定一个模板串, 再给出\(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. mongodb v2.4.9 快速操作记录

    参考链接:http://www.runoob.com/mongodb/mongodb-tutorial.html oschina链接:https://gitee.com/dhclly/icedog.s ...

  2. KVO And KVC

    http://www.cocoachina.com/industry/20140224/7866.html

  3. shell批量修改文件名

    [root@localhost file1]# ls a.htm b.htm c.htm d.htm pl.sh [root@localhost file1]# vi pl.sh #!/bin/bas ...

  4. 记一次WMS的系统改造(3)— 行进中的复盘

    行进中的波折 革新总会面对一些阻力和风险,一种新的观念.一种新的模式要来替代既有的产品,从来都不是一件简单的事,在WMS改造这件事上我们一开始就提出两种概念货物驱动和任务驱动,并找到一个标杆产品Sla ...

  5. Selenium WebDriver-通过断言页面是否存在某些关键字来确定页面按照预期加载

    #encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...

  6. 以前刷过的FFT

    Gym - 101667H 2017-2018 ACM-ICPC, Asia Daejeon Regional Contest #include<bits/stdc++.h> using ...

  7. hibernate基础工具findBySQL学习

    public List<Map<String,Object>> findBySQL(String sql,Map<String,Object> param,int ...

  8. 【bzoj3261】最大异或和 可持久化Trie树

    题目描述 给定一个非负整数序列 {a},初始长度为 N.       有M个操作,有以下两种操作类型:1.A x:添加操作,表示在序列末尾添加一个数 x,序列的长度 N+1.2.Q l r x:询问操 ...

  9. css的部分应用示例

    CSS :层叠样式表,Cascading Style Sheets.CSS不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化. 1 vertical-align 在图片与文字对 ...

  10. 阿狸的打字机(bzoj 2434)

    Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...