题目链接:Codeforces 235C Cyclical Quest

代码

#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
const int SIGMA_SIZE = 26;struct SAM {int sz, last;int g[maxn<<1][SIGMA_SIZE], pre[maxn<<1], step[maxn<<1];int pos[maxn<<1], right[maxn<<1], cnt[maxn<<1], mark[maxn<<1];void newNode(int s) {step[++sz] = s;pre[sz] = 0;memset(g[sz], 0, sizeof(g[sz]));}int idx(char ch) { return ch -'a'; }void init() {sz = 0, last = 1;newNode(0);}void insert(char ch);void get_tuopu();void get_right(char* str);void presolve() {for (int i = 1; i <= sz; i++) {int u = pos[i];printf("%d ", right[u]);}printf("\n");}int solve(char* str, int k, int mk) {int ret = 0;int p = 1, n = strlen(str), lcs = 0;for (int i = 0; i < n; i++) {int v = idx(str[i]);if (g[p][v]) { lcs++, p = g[p][v]; }else {while (p && g[p][v] == 0) p = pre[p];if (p) lcs = step[p] + 1, p = g[p][v];else p = 1, lcs = 0;}if (lcs >= k) {while (pre[p] && step[pre[p]] >= k) p = pre[p], lcs = step[p];if (mark[p] != mk) {mark[p] = mk;ret += right[p];}}}return ret;}
}SA;char str[maxn], a[maxn];int main () {scanf("%s", str);SA.init();int n = strlen(str);for (int i = 0; i < n; i++) SA.insert(str[i]);SA.get_tuopu();SA.get_right(str);scanf("%d", &n);for (int k = 1; k <= n; k++) {scanf("%s", a);int n = strlen(a), p = 0;for (int i = 0; i < n; i++) str[p++] = a[i];for (int i = 0; i < n-1; i++) str[p++] = a[i];str[p++] = '\0';printf("%d\n", SA.solve(str, n, k));}return 0;
}void SAM::insert(char ch) {newNode(step[last] + 1);int v = idx(ch), p = last, np = sz;while (p && !g[p][v]) {g[p][v] = np;p = pre[p];}if (p) {int q = g[p][v];if (step[q] == step[p] + 1)pre[np] = q;else {newNode(step[p] + 1);int nq = sz;for (int j = 0; j < SIGMA_SIZE; j++) g[nq][j] = g[q][j];pre[nq] = pre[q];pre[np] = pre[q] = nq;while (p && g[p][v] == q) {g[p][v] = nq;p = pre[p];}}} elsepre[np] = 1;last = np;
}void SAM::get_tuopu() {for (int i = 0; i <= sz; i++) cnt[i] = 0;for (int i = 1; i <= sz; i++) cnt[step[i]]++;for (int i = 1; i <= sz; i++) cnt[i] += cnt[i-1];for (int i = 1; i <= sz; i++) pos[cnt[step[i]]--] = i;
}void SAM::get_right(char* str) {int p = 1, n = strlen(str);for (int i = 0; i <= sz; i++) right[i] = 0;for (int i = 0; i < n; i++) {int v = idx(str[i]);p = g[p][v];right[p]++;}for (int i = sz; i; i--) {int u = pos[i];right[pre[u]] += right[u];}
}

Codeforces 235C Cyclical Quest(后缀自动机)相关推荐

  1. Codeforces 235C Cyclical Quest (后缀自动机)

    题目链接: https://codeforces.com/contest/235/problem/C 题解: 对大串建后缀自动机 对询问串复制拆环.这里一定要注意是复制一个循环节不是复制整个串!循环节 ...

  2. CodeForces 235C Cyclical Quest (后缀自动机)

    题意:给一个主串,再给出多个模式串,分别求主串中有多少个连续子串,与模式串循环同构. 题解:后缀自动机 因为要求循环同构,所以将模式串复制放到后面.(要么加终止符,要么传入长度) 先对主串建sam,然 ...

  3. Codeforces #235 C.Cyclical Quest(后缀自动机)

    传送门 题意:给定一个模式串和nnn个匹配串,询问原串有多少个子串和匹配串循环同构 考虑要求循环同构,于是先对SSS建出后缀自动机 把每次询问的XXX倍长在自动机上跑 如果当前匹配的长度已经超过原串长 ...

  4. Codeforces.700E.Cool Slogans(后缀自动机 线段树合并 DP)

    题目链接 \(Description\) 给定一个字符串\(s[1]\).一个字符串序列\(s[\ ]\)满足\(s[i]\)至少在\(s[i-1]\)中出现过两次(\(i\geq 2\)).求最大的 ...

  5. 后缀自动机(探索)Codeforces 427D

    想学后缀自动机的 弱鸡 表示真自闭啊 开局一道题,内容全靠水 :Codeforces 427D 有题目才能更好地学 算法 题意很简单 :给两个字符串,求最短公共子串 的长度 后缀自动机 模板很多 ,给 ...

  6. CodeForces - 427D Match Catch(后缀数组/广义后缀自动机)

    题目链接:点击查看 题目大意:给出两个字符串,求出两个字符串中的最短公共子串,且在每个字符串中只出现过一次 题目分析:因为这个公共子串只能在字符串中出现一次,考虑到用后缀数组,我们先将两个字符串通过特 ...

  7. Codeforces Round #364 (Div. 1) (差一个后缀自动机)

    B. Connecting Universities 大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值 单独考虑每条边$(u,v)$的贡献即可, ...

  8. Codeforces 235C

    Codeforces 235C 题目:给定一主串\(S\),\(n\)次询问,每次询问串\(t\)的所有循环移位串的出现的次数和 做法:建\(SAM\),对于询问串\(t\),将他复制一份放在后边,在 ...

  9. CF 316G3 Good Substrings——广义后缀自动机

    题目:http://codeforces.com/contest/316/problem/G3 对询问串和模式串一起建一个后缀自动机,做出在每个串上的 right 集合大小之后枚举自动机上的每个点看看 ...

  10. 【POJ1509】Glass Beads 【后缀自动机】

    题意 给出一个字符串,求它的最小表示法. 分析 这个题当然可以用最小表示法做啦!但是我是为了学后缀自动机鸭! 我们把这个字符串长度乘二,然后建SAM,然后在SAM上每次跑最小的那个字母,找出长度为n的 ...

最新文章

  1. Spring 详解(五):Spring声明式事务
  2. Ubuntu 安装 Qt 开发环境 简单实现
  3. 透过【百度地图API】分析双闭包问题
  4. 程序员面试金典 - 面试题 17.15. 最长单词(排序+递归)
  5. android系统手势app,8种iOS手势规定和14种android手势规定详解
  6. linux应用程序逆向,Linux下查看并下载命令源码包(根据命令/应用程序逆向获取并且安装其所属源码包)...
  7. Problem E:结构体---点坐标结构体
  8. Codewars-Regex validate PIN code(正则检验PIN码)
  9. call 在mysql,在MYSQL上选择CASE和CALL程序
  10. 内存问题分析工具_valgrind之memcheck基本使用
  11. Bzoj1103 [POI2007]大都市meg
  12. Ubuntu下安装配置Phabricator
  13. curl的安装及使用
  14. 模式识别--绪论 什么是模式识别?模式识别的主要方法及具体应用
  15. CSDN日报191021:我与CSDN的这十年——笔耕不辍,青春热血
  16. 计算机在化学中论文3000字,化学论文范文3000字_化学论文发表
  17. 小米口碑营销案例的十大秘诀
  18. 解读华为云应用平台ROMA,黑科技实现一站式政企上云
  19. 使用spool导出数据
  20. 128Echarts - 关系图(NPM Dependencies)

热门文章

  1. 同样是路过式,登录与下载攻击区别何在?
  2. Pocket PC 2003 html 的问题
  3. 百度AI的2020:迎合时代节拍,扛起智能大旗
  4. 西游记中最顶尖的妖怪
  5. 如何理解面向对象(POO)?
  6. Yolov5的配置+训练(超级详细!!!)
  7. 【其他】计蒜客 ICPC Pacific Northwest Regional Contest 2017 Gym-101652X Star Arrangements
  8. HEVC函数入门(22)——变换量化
  9. Jetson Tegra X系列刷机教程
  10. 浅谈C++11中的move和forward