当前位置:网站首页>[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;
}
边栏推荐
- QML combines qsqltablemodel to dynamically load data MVC "recommended collection"
- Google pixel 6A off screen fingerprint scanner has major security vulnerabilities
- 智能电子界桩自然保护区远程监控解决方案
- Aircraft PID control (rotor flight control)
- RF, gbdt, xgboost feature selection methods "recommended collection"
- wallys//IPQ5018/IPQ6010/PD-60 802.3AT Input Output 10/100/1000M
- Advantages of network virtualization of various manufacturers
- 10.< tag-动态规划和子序列, 子数组>lt.53. 最大子数组和 + lt.392. 判断子序列 dbc
- 网络协议:TCP Part2
- Pytorch's transforms (numpy data type is converted to tensor, normalized and resized)
猜你喜欢
![[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now

Prescan quick start to master Lesson 19: prescan actuator configuration, track synchronization and non configuration of multiple tracks

分享 25 个有用的 JS 单行代码

4. Server startup of source code analysis of Nacos configuration center

PMP采用最新考纲,这里有【敏捷项目管理】
![[cloud native | learn kubernetes from scratch] VIII. Namespace resource quotas and labels](/img/7e/2bdead512ba5bf5ccd0830b0f9b0f2.png)
[cloud native | learn kubernetes from scratch] VIII. Namespace resource quotas and labels
![[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference](/img/0f/8ce2d5487b16d38a152cfd3ab454bb.png)
[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference
![[advanced mathematics] [4] indefinite integral](/img/4f/2aae654599fcc0ee85cb1ba46c9afd.png)
[advanced mathematics] [4] indefinite integral

导电滑环在机械设备方面的应用
![[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
随机推荐
[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
Arrow 之 Parquet
[today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace
CarSim仿真快速入门(十六)—CarSim传感器仿真之ADAS Sensor Objects (2)
Apache Mina framework "suggestions collection"
PMP adopts the latest exam outline, here is [agile project management]
10. < tag dynamic programming and subsequence, subarray> lt.53. maximum subarray and + lt.392. Judge subsequence DBC
【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
网络RTK无人机上机测试[通俗易懂]
Pytorch's transforms (numpy data type is converted to tensor, normalized and resized)
PMP采用最新考纲,这里有【敏捷项目管理】
飞行器pid控制(旋翼飞控)
Rainbow plug-in extension: monitor MySQL based on MySQL exporter
How does tiktok break zero?
当AI邂逅生命健康,华为云为他们搭建三座桥
RF、GBDT、XGboost特征选择方法「建议收藏」
test
CarSim仿真快速入门(十四)—CarSim-Simulink联合仿真
第六章 修改规范(SPEC)类
分享 25 个有用的 JS 单行代码