当前位置:网站首页>[noi simulation] string matching (suffix automata Sam, Mo team, block)
[noi simulation] string matching (suffix automata Sam, Mo team, block)
2022-07-25 20:18:00 【DD(XYX)】
Topic
1024MB,4s


Answer key
We build text strings into suffix automata , Then take the inquiry offline , Press the right endpoint to place it on the pattern string , Then you can find the pattern string where each prefix is SAM Corresponding node on , Get the number of occurrences of the substring , Update answers . This is O ( n 2 ) O(n^2) O(n2) Of .
We can use the Mo team Algorithm , First preprocess each point on the suffix tree p p p The largest of our ancestors t × l e n t\times len t×len , And then because of The extended intervals in Mo's algorithm are all definite , We can put the interval in Mo team again “ offline ” To suffix tree , Calculate the corresponding SAM The points on the p p p , Contribution is p p p Times the current p p p The effective length of , as well as p p p The largest of our ancestors t × l e n t\times len t×len . Time complexity O ( n n ) O(n\sqrt n) O(nn) .
But it's a little slow , We can actually prune directly with violence .
Go back to that O ( n 2 ) O(n^2) O(n2) How to do it , We get the corresponding point of the current prefix p p p after , stay l e n [ p ] ∼ l e n [ f a [ p ] ] − 1 len[p]\sim len[fa[p]]-1 len[p]∼len[fa[p]]−1 Enumeration between the contributions , But in the process, if the answer is less than p p p The largest of our ancestors t × l e n t\times len t×len , You can just jump out of it . in addition , Don't jump one step at a time, father , You can jump directly to t [ q ] × l e n [ q ] t[q]\times len[q] t[q]×len[q] maximal p p p The ancestors of the q q q . It can be proved that the upper limit of skipping ancestors is O ( n ) O(\sqrt n) O(n) Of , The number of violent enumeration is also very small , It is related to the size of the character set . The contribution of each enumeration is maintained in blocks , O ( 1 ) O(1) O(1) modify , O ( n ) O(\sqrt n) O(n) Inquire about . This problem is limited to four seconds , This method only takes 500 milliseconds to round .
CODE
#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<random>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#pragma GCC optimize(2)
using namespace std;
#define MAXN 100005
#define LL long long
#define ULL unsigned long long
#define ENDL putchar('\n')
#define DB double
#define lowbit(x) (-(x) & (x))
#define FI first
#define SE second
#define PR pair<int,int>
#define UIN unsigned int
#define SQ 317
int xchar() {
static const int maxn = 1000000;
static char b[maxn];
static int pos = 0,len = 0;
if(pos == len) pos = 0,len = fread(b,1,maxn,stdin);
if(pos == len) return -1;
return b[pos ++];
}
// #define getchar() xchar()
inline LL read() {
LL f = 1,x = 0;int s = getchar();
while(s < '0' || s > '9') {
if(s<0)return -1;if(s=='-')f=-f;s = getchar();}
while(s >= '0' && s <= '9') {
x = (x<<1) + (x<<3) + (s^48);s = getchar();}
return f*x;
}
void putpos(LL x) {
if(!x)return ;putpos(x/10);putchar((x%10)^48);}
inline void putnum(LL x) {
if(!x) {
putchar('0');return ;}
if(x<0) putchar('-'),x = -x;
return putpos(x);
}
inline void AIput(LL x,int c) {
putnum(x);putchar(c);}
int n,m,s,o,k;
inline LL Max(LL a,LL b) {
return a>b ? a:b;}
char s1[MAXN],s2[MAXN];
struct SAM{
int s[26],len,fa;
SAM(){
memset(s,0,sizeof(s));len=fa=0;}
}sam[MAXN<<1];
int las = 1,cnt = 1;
int addsam(int c) {
int p = las,np = (las = ++ cnt);
sam[np].len = sam[p].len + 1;
for(;p&&!sam[p].s[c];p=sam[p].fa)sam[p].s[c] = np;
if(!p) sam[np].fa = 1;
else {
int q = sam[p].s[c];
if(sam[q].len == sam[p].len + 1) sam[np].fa = q;
else {
int nq = ++ cnt; sam[nq] = sam[q];
sam[nq].len = sam[p].len + 1;
sam[np].fa = sam[q].fa = nq;
for(;p&&sam[p].s[c]==q;p=sam[p].fa)sam[p].s[c]=nq;
}
}return np;
}
vector<int> g[MAXN<<1];
int ct[MAXN<<1],tp[MAXN<<1];
LL wt[MAXN<<1];
void dfs(int x) {
for(int y:g[x])dfs(y),ct[x]+=ct[y];}
void dfs2(int x) {
wt[x] = sam[x].len *1ll* ct[x];
tp[x] = sam[x].fa;
if(wt[tp[sam[x].fa]] >= wt[tp[x]]) tp[x] = tp[sam[x].fa];
for(int y:g[x]) dfs2(y);
}
int bl[MAXN];
LL mw[MAXN],mb[MAXN];
inline void addp(int x,LL y) {
mw[x] = Max(mw[x],y);
mb[bl[x]] = Max(mb[bl[x]],y);
}
int ql[MAXN];
vector<int> bu[MAXN];
LL as[MAXN];
int main() {
scanf("%s",s1 + 1);
n = strlen(s1 + 1);
for(int i = 1;i <= n;i ++) {
bl[i] = i/SQ+1;
}
scanf("%s",s2 + 1);
m = strlen(s2 + 1);
for(int i = 1;i <= m;i ++) {
s = addsam(s2[i]-'a');
ct[s] ++;
}
for(int i = 2;i <= cnt;i ++) g[sam[i].fa].push_back(i);
dfs(1); dfs2(1);
int Q = read(),flag = 1;
for(int i = 1;i <= Q;i ++) {
ql[i] = read();o = read();
bu[o].push_back(i);
if(ql[i] > 1) flag = 0;
}
if(flag) {
LL ans = 0;
int p = 1,le = 0;
for(int i = 1;i <= n;i ++) {
int c = s1[i]-'a';
while(p > 1 && !sam[p].s[c]) p = sam[p].fa,le = sam[p].len;
if(sam[p].s[c]) p = sam[p].s[c],le ++;
ans = max(ans,le*1ll*ct[p]);
ans = max(ans,wt[tp[p]]);
for(int x:bu[i]) {
as[x] = ans;
}
}
for(int i = 1;i <= Q;i ++) {
AIput(as[i],'\n');
} return 0;
}
int p = 1,le = 0;
for(int i = 1;i <= n;i ++) {
int c = s1[i]-'a';
while(p > 1 && !sam[p].s[c]) p = sam[p].fa,le = sam[p].len;
if(sam[p].s[c]) p = sam[p].s[c],le ++;
int pp = p,ll = le;
while(pp) {
int nx = tp[pp];
for(;ll > sam[sam[pp].fa].len && ll*1ll*ct[pp] > wt[nx];ll --) {
addp(i-ll+1,ll*1ll*ct[pp]);
} pp = nx; ll = sam[pp].len;
}
for(int x:bu[i]) {
s = ql[x];
for(int j = s;bl[j] == bl[s];j ++) as[x] = Max(as[x],mw[j]);
for(int j = bl[s]+1;j <= bl[n];j ++) as[x] = Max(as[x],mb[j]);
}
}
for(int i = 1;i <= Q;i ++) {
AIput(as[i],'\n');
}
return 0;
}
边栏推荐
- Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
- tiktok如何破零播放?
- [today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
- 【云原生 | 从零开始学Kubernetes】八、命名空间资源配额以及标签
- PMP采用最新考纲,这里有【敏捷项目管理】
- Cloud native guide: what is cloud native infrastructure
- wallys//IPQ5018/IPQ6010/PD-60 802.3AT Input Output 10/100/1000M
- [today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace
- [today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
- Rainbow plug-in extension: monitor MySQL based on MySQL exporter
猜你喜欢

sentinel简单限流和降级demo问题记录

PreScan快速入门到精通第十八讲之PreScan轨迹编辑的特殊功能

增加 swap 空间

谷歌Pixel 6a屏下指纹扫描仪存在重大安全漏洞

Do you still have certificates to participate in the open source community?
![[advanced mathematics] [8] differential equation](/img/83/b6b07540e3cf6d6433e57447d42ee9.png)
[advanced mathematics] [8] differential equation

Prescan quick start to master the special functions of prescan track editing in lecture 18
![Summarize the level of intelligent manufacturing discussion [macro understanding]](/img/84/3addabdf857c562535a4085782d3e8.png)
Summarize the level of intelligent manufacturing discussion [macro understanding]

YOLOv7论文部分解读【含自己的理解】

How does tiktok break zero?
随机推荐
网络爬虫原理解析「建议收藏」
Proxy implements MySQL read / write separation
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
What is cluster analysis? Categories of cluster analysis methods [easy to understand]
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
毕业从事弱电3个月,我为什么会选择转行网络工程师
YOLOv7论文部分解读【含自己的理解】
10.< tag-动态规划和子序列, 子数组>lt.53. 最大子数组和 + lt.392. 判断子序列 dbc
Prescan quick start to master the special functions of prescan track editing in lecture 18
当AI邂逅生命健康,华为云为他们搭建三座桥
接口请求合并的3种技巧,性能直接爆表!
什么是聚类分析?聚类分析方法的类别[通俗易懂]
LP dual currency pledge liquidity mining DAPP system development logic analysis
Error when creating dataset with mindscore
[advanced mathematics] [5] definite integral and its application
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born
谷歌Pixel 6a屏下指纹扫描仪存在重大安全漏洞
Working principle of radar water level gauge and precautions for installation and maintenance
How does tiktok break zero?
Recommendations on how to install plug-ins and baby plug-ins in idea