HDU 4641 K-string (SAM)
来源:互联网 发布:js金沙娱乐190768 编辑:程序博客网 时间:2024/05/21 19:25
Description
Given a string S. K-string is the sub-string of S and it appear in the S at least K times.It means there are at least K different pairs (i,j) so that Si,Si+1… Sj equal to this K-string. Given m operator or query:1.add a letter to the end of S; 2.query how many different K-string currently.For each query ,count the number of different K-string currently.
Input
The input consists of multiple test cases.
Each test case begins with a line containing three integers n, m and K(1<=n,K<=50000,1<=m<=200000), denoting the length of string S, the number of operator or question and the least number of occurrences of K-string in the S.
The second line consists string S,which only contains lowercase letters.
The next m lines describe the operator or query.The description of the operator looks as two space-separated integers t c (t = 1; c is lowercase letter).The description of the query looks as one integer t (t = 2).
Output
For each query print an integer — the number of different K-string currently.
Sample Input
3 5 2abc21 a21 a2
Sample Output
011
题意
对于一个长度为
- 向字符串的末尾增加一个字符
c - 查询串中至少出现
m 次的子串有多少个
思路
我们用原串建立后缀自动机,显然第一种操作即一次 insert
。
对于第二种操作,我们考虑每一次的 insert
,对新加入节点
最终的
AC 代码
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int maxn = 5e5+10;LL ans, K;struct SAM{ int ch[maxn][26]; int pre[maxn], step[maxn]; int last, id; int num[maxn]; void init() { last = id = 0; memset(ch[0], -1, sizeof(ch[0])); pre[0] = -1; step[0] = 0; } void insert(int c) { int p = last, np = ++id; step[np] = step[p] + 1; memset(ch[np], -1, sizeof(ch[np])); num[np] = 0; while (p != -1 && ch[p][c] == -1) ch[p][c] = np, p = pre[p]; if (p == -1) pre[np] = 0; else { int q = ch[p][c]; if (step[q] != step[p] + 1) { int nq = ++id; memcpy(ch[nq], ch[q], sizeof(ch[q])); num[nq] = num[q]; step[nq] = step[p] + 1; pre[nq] = pre[q]; pre[np] = pre[q] = nq; while (p != -1 && ch[p][c] == q) ch[p][c] = nq, p = pre[p]; } else pre[np] = q; } last = np; /* diy */ while (np != -1 && num[np] < K) { num[np]++; if (num[np] >= K) ans += step[np] - step[pre[np]]; np = pre[np]; } /* end diy */ }} sam;char str[maxn];int main(){ int n, q; while (~scanf("%d%d%I64d%*c", &n, &q, &K)) { sam.init(); ans = 0; gets(str); for (int i = 0; i < n; i++) sam.insert(str[i] - 'a'); for (int i = 0; i < q; i++) { int op; scanf("%d%*c", &op); if (op == 1) { char c; c = getchar(); sam.insert(c - 'a'); } else printf("%I64d\n", ans); } } return 0;}
- HDU 4641 K-string (SAM)
- hdu 4641 K-string (sam)
- HDU 6194 string string string (SAM)
- HDU 4641 SAM
- hdu 4622 Reincarnation(SAM)
- hdu 4641 K-string(后缀自动机 + 并查集)
- 【后缀自动机】[HDU 4641]K-string
- hdu 4436 str2int (SAM)(待补)
- 2013多校联合4 1010 K-string (hdu 4641)
- 【2016-CCPC-K】水题(Lweb and String,hdu 5842)
- HDU 4622 Reincarnation SAM
- hdu 4436 str2int (SAM)
- HDU 4641 sam…………好恶心
- hdu6194 string string string sam或sa
- HDU 4436 str2int(12年天津,SAM)
- HDU 3518 Boring counting(后缀自动机 SAM)
- hdu 4416 Good Article Good sentence (后缀自动机 SAM)
- HDU 4436 str2int (后缀自动机SAM,多串建立)
- ES6的新特性
- 1、Nginx和Tengine概述及安装、启动
- cocos2dx CCMenu.h:error: 'override' does not name a type
- CSS 移动端
- 安装centos与配置IP地址,远程登录
- HDU 4641 K-string (SAM)
- Nginx反向代理配置示例
- Servlet与Jsp学习指南-1
- Java中集合(四)LinkedList
- 用矩阵快速幂求斐波那契数列
- hdu 1863 [【最小生成树】+hdu2544【floyed】+hdu1874【dijdtra】~~~~模板复习~~~
- BZOJ 4922 Karp-de-Chant Number 贪心+动态规划
- Android——ViewPager无限循环滑动
- TensorFlow 学习率的设置