HDU - 4622 Reincarnation(后缀自动机-查询区间本质不同子串个数)
题目链接:点击查看
题目大意:给出一个长度为 n 的字符串,再给出 q 个询问,每次询问需要回答区间 [ l , r ] 内有多少个本质不同的子串
题目分析:和回文自动机那个题目一样,n * n 预处理出一个答案数组,然后 O( 1 ) 查询就好了
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e3+100;char s[N];int tot,last,ans[N][N];struct Node
{int ch[26];int fa,len;
}st[N<<1];inline int newnode()
{tot++;for(int i=0;i<26;i++)st[tot].ch[i]=0;st[tot].fa=st[tot].len=0;return tot;
}void add(int x)
{int p=last,np=last=newnode();st[np].len=st[p].len+1;while(p&&!st[p].ch[x])st[p].ch[x]=np,p=st[p].fa;if(!p)st[np].fa=1;else{int q=st[p].ch[x];if(st[p].len+1==st[q].len)st[np].fa=q;else{int nq=newnode();st[nq]=st[q]; st[nq].len=st[p].len+1;st[q].fa=st[np].fa=nq;while(p&&st[p].ch[x]==q)st[p].ch[x]=nq,p=st[p].fa;//向上把所有q都替换成nq}}
}void init()
{last=1;tot=0;newnode();
}int main()
{
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int w;cin>>w;while(w--){scanf("%s",s);int n=strlen(s);for(int i=0;i<n;i++){init();int sum=0;for(int j=i;j<n;j++){add(s[j]-'a');sum+=st[last].len-st[st[last].fa].len;ans[i][j]=sum;}}int q;scanf("%d",&q);while(q--){int l,r;scanf("%d%d",&l,&r);l--,r--;printf("%d\n",ans[l][r]);}}return 0;
}
HDU - 4622 Reincarnation(后缀自动机-查询区间本质不同子串个数)相关推荐
- 洛谷 - P6292 区间本质不同子串个数(SAM+LCT+线段树)
题目链接:点击查看 题目大意:给出一个长度为 n 的字符串,再给出 m 次询问,每次询问需要回答区间 [ l , r ] 内有多少个本质不同的字符串 题目分析:首先简化模型,回顾一下如何求解 &quo ...
- hdu 4622 Reincarnation SAM模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有Q次区间查询(Q <= 10000),问区 ...
- hdu 2665(主席树查询区间k大值)
先贴我自己写的代码做模板虽然跟原博主没什么两样.(一开始空间开的4*maxn,交到hdu上一直TLE很奇怪) #include<bits/stdc++.h> using namespace ...
- HDU 3518 HDU 4416【后缀自动机len的使用】
max:即代码中 len 变量,它表示该状态能够接受的最长的字符串长度. min:表示该状态能够接受的最短的字符串长度.实际上等于该状态的 fail 指针指向的结点的 len + 1. max-min ...
- hdu 4417 树状数组查询区间不是1到n时需要转换,例[0,5]变成[1,6]
http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意:求区间内小于等于h的数的个数. 方法: 先对输入的n个数由小到大排序,在对查询的h由小到大排序. # ...
- HDU - 4641 K-string(后缀自动机)
题目链接:点击查看 题目大意:给出一个字符串 s ,规定 K-string 为字符串 s 中出现次数大于等于 k 次的字串,现在有 m 次操作,每次操作有两种: 1 ch:在字符串 s 后面添加字符 ...
- Just $h$-index HDU - 6278(主席树找区间大于等于k的个数)
The hh-index of an author is the largest hh where he has at least hh papers with citations not less ...
- SPOJ7258(后缀自动机--第k大的子串)
题目:http://www.spoj.com/problems/SUBLEX/ 题意:给一个字符串,然后给Q个询问,每个询问输入一个数K,输出子串中字典序为K的字符串. #include <io ...
- SPOJ8222(后缀自动机--出现次数最多的子串)
题目:http://www.spoj.com/problems/NSUBSTR/ 题意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.求F(1)..F(Length(S)) ...
最新文章
- Singleton(单例模式)
- python如何训练模型生产_手把手教你用Python构建你的第一个多标签图像分类模型(附案例)...
- 初步了解WPF依赖属性
- 程序员过关斩将--少年派登录安全的奇幻遐想
- 如何处理班级过多的问题
- 苹果服务两天内经历两次宕机:部分服务受影响 现已修复
- python3.3psutil模块安装_详解Python3.6安装psutil模块和功能简介
- t470换屏线_thinkpad t470怎么样?thinkpad t470拆机图解全面评测
- RSA算法和SM2算法对比
- 2021编辑器Eclipse汉化中文教程
- 合作博弈:夏普利值(shapley value)性质与算法
- 数据结构基础知识——非线性数据结构(二叉树、二叉排序树、优先队列、散列表)
- 与门,或门,与非门,异或门的python实现
- cf1009 C. Annoying Present
- C3P0、狂生说MySQL、: No suitable driver
- python3发起一个http请求
- 第二届安洵杯2019部分writeup
- 梦幻西游 python.dll_GitHub - 10508zhl/mhxy: tensorflow实践:梦幻西游人物弹窗识别
- 一篇就够,线程与线程池的那些事之线程池篇
- DeepLearning4J入门——让计算机阅读《天龙八部》