当前位置:网站首页>[Luogu p5410] [template] extend KMP (Z function) (string)
[Luogu p5410] [template] extend KMP (Z function) (string)
2022-07-24 09:12:00 【SSL_ TJH】
【 Templates 】 Expand KMP(Z function )
Topic link :luogu P5410
The main idea of the topic
Give you a string , I want you to ask each suffix to follow it LCP The length of .(Z function )
Here are two strings , Ask you to find a string, each suffix with another string LCP The length of .(exKMP)
Ideas
This thing feels like a horse pulling a cart .
Consider knowing the current answer z 1 ∼ i − 1 z_{1\sim i-1} z1∼i−1, Consider how to ask for z i z_i zi.
First of all, we must find a matching string [ l , r ] [l,r] [l,r], That must be looking for r r r maximal , The record is currently l , r l,r l,r.
And then if i ⩽ r i\leqslant r i⩽r, We consider using it , Through proof, we can get r i ⩾ min ( r − i + 1 , r i − l + 1 ) r_i\geqslant \min(r-i+1,r_{i-l+1}) ri⩾min(r−i+1,ri−l+1)
Proof considers bit by bit proof , set up 1 ⩽ x ⩽ min ( r − i + 1 , r i − l + 1 ) 1\leqslant x\leqslant \min(r-i+1,r_{i-l+1}) 1⩽x⩽min(r−i+1,ri−l+1)
s i + x − 1 = s l + ( i + x − l ) − 1 s_{i+x-1}=s_{l+(i+x-l)-1} si+x−1=sl+(i+x−l)−1
= s 1 + ( i + x − l ) − 1 = s i + x − l =s_{1+(i+x-l)-1}=s_{i+x-l} =s1+(i+x−l)−1=si+x−l( The restrictions on the left )
= s 1 + ( i − l + 1 ) + x =s_{1+(i-l+1)+x} =s1+(i−l+1)+x
= s 1 + x =s_{1+x} =s1+x( When x ≤ z i − l + 1 x\leq z_{i-l+1} x≤zi−l+1)
Then we expand the violence , Then maintain the maximum r r r For the l , r l,r l,r.
Then each number will only expand once , So the complexity is O ( n ) O(n) O(n).
As for between two characters exKMP, We have the same matching method .
Code
#include<cstdio>
#include<cstring>
#include<iostream>
#define ll long long
using namespace std;
const int N = 2e7 + 5;
char a[N], b[N];
int an, bn, z[N], p[N];
ll ans;
void get_Z(char *s, int n) {
for (int i = 1; i <= n; i++) z[i] = 0;
z[1] = n;
for (int i = 2, l = 0, r = 0; i <= n; i++) {
if (i <= r) z[i] = min(z[i - l + 1], r - i + 1);
while (i + z[i] <= n && s[i + z[i]] == s[1 + z[i]]) z[i]++;
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
}
void exKMP(char *a, int n, char *b, int m) {
for (int i = 1; i <= n; i++) p[i] = 0;
for (int i = 1, l = 0, r = 0; i <= n; i++) {
if (i <= r) p[i] = min(z[i - l + 1], r - i + 1);
while (i + p[i] <= n && a[i + p[i]] == b[1 + p[i]]) p[i]++;
if (i + p[i] - 1 > r) l = i, r = i + p[i] - 1;
}
}
int main() {
scanf("%s", a + 1); scanf("%s", b + 1);
an = strlen(a + 1); bn = strlen(b + 1);
get_Z(b, bn);
ans = 0; for (int i = 1; i <= bn; i++) ans ^= 1ll * i * (z[i] + 1); printf("%lld\n", ans);
exKMP(a, an, b, bn);
ans = 0; for (int i = 1; i <= an; i++) ans ^= 1ll * i * (p[i] + 1); printf("%lld", ans);
return 0;
}
边栏推荐
- TCP triple handshake connection combing
- Promise basic summary
- Taking advantage of the momentum, oceanbase promotes the lean growth of digital payment
- 超全总结:Go语言如何操作文件
- Tiktok shop will add a new site, and the Singapore site will be launched on June 9
- [the first anniversary of my creation] love needs to be commemorated, so does creation
- Why does TCP shake hands three times instead of two times (positive version)
- Six pictures show you why TCP shakes three times?
- Code random notes_ Linked list_ Turn over the linked list in groups of 25K
- Houdini notes
猜你喜欢
随机推荐
Android system security - 5.3-apk V2 signature introduction
Tiflash source code reading (V) deltatree storage engine design and implementation analysis - Part 2
Unity solves the package manager "you see to be offline"
Definition and initialization of cv:: mat
Six pictures show you why TCP shakes three times?
S2b2b system standardizes the ordering and purchasing process and upgrades the supply chain system of household building materials industry
Assignment operator (geritilent software - Jiuye training)
Super complete summary: how to operate files in go language
Learning transformer: overall architecture and Implementation
SQL optimization principles
链表——24. 两两交换链表中的节点
Data collection solution for forestry survey and patrol inspection
Code random notes_ Linked list_ Turn over the linked list in groups of 25K
Tongxin UOS developer account has supported uploading the HAP package format of Hongmeng application
Leetcode94-二叉树的中序遍历详解
SQL 优化原则
Promise basic summary
Promise基础总结
[translation] integration challenges in microservice architecture using grpc and rest
代码随想录笔记_链表_25K个一组翻转链表



![[FFH] websocket practice of real-time chat room](/img/9a/ffd31fe8783804d40edeca515cc96c.png)





