当前位置:网站首页>[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;
}
边栏推荐
- UNET and mask RCNN
- [today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
- Introduction and construction of consul Registration Center
- 项目中new Promise和async、await中的使用,以及promise.all在项目中的实际应用
- [today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
- RF、GBDT、XGboost特征选择方法「建议收藏」
- [mindspore] [read graph data] cannot read mindrecord format graph data
- Detailed evaluation of current popular redis visual management tools
- tiktok如何破零播放?
- From Tong Dai to "Tong Dai" and then to brand, the beauty of sudden profits has changed and remained unchanged
猜你喜欢

雷达水位计的工作原理及安装维护注意事项
![[advanced mathematics] [6] differential calculus of multivariate functions](/img/9e/84fe6f74b58cbaabab1b6eed0df556.png)
[advanced mathematics] [6] differential calculus of multivariate functions

笔记——记录一个CannotFindDataSourceException: dynamic-datasource can not find primary datasource问题解决
![[today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal

【高等数学】【4】不定积分

【高等数学】【1】函数、极限、连续
Detailed evaluation of current popular redis visual management tools

Jmeter——接口测试
![[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

Application of conductive slip ring in mechanical equipment
随机推荐
From Tong Dai to "Tong Dai" and then to brand, the beauty of sudden profits has changed and remained unchanged
Stochastic gradient descent method, Newton method, impulse method, adagrad, rmsprop and Adam optimization process and understanding
4、Nacos 配置中心源码解析之 服务端启动
Difference Between Accuracy and Precision
火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布
Jmeter——接口测试
MySQL date [plus sign / +] condition filtering problem
sentinel简单限流和降级demo问题记录
导电滑环在机械设备方面的应用
DIY personal server (DIY storage server)
Google pixel 6A off screen fingerprint scanner has major security vulnerabilities
"Share" devaxpress asp Net v22.1 latest version system environment configuration requirements
JS作用域与作用域链
网络RTK无人机上机测试[通俗易懂]
Apache Mina framework "suggestions collection"
[today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace
CarSim仿真快速入门(十五)—CarSim传感器仿真之ADAS Sensor Objects (1)
从瞳代到“瞳代”再到品牌,暴利的美瞳的变与未变
[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
QQ是32位还是64位软件(在哪看电脑是32位还是64位)