当前位置:网站首页>[Luogu p5829] [template] mismatch tree (string) (KMP)
[Luogu p5829] [template] mismatch tree (string) (KMP)
2022-07-24 09:13:00 【SSL_ TJH】
【 Templates 】 Mismatch tree
Topic link :luogu P5829
The main idea of the topic
Give you a string , Ask you for the longest public of the two prefixes many times border The length of .
One border A substring is both a prefix and a suffix .
Ideas
in consideration of border Namely fail A chain on the tree .
The public is LCA!
Just get it out .
And then it's important to note that border It can't be yourself , So we have to make a special judgment. If one of the two wants to jump to his father .
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e6 + 100;
char s[N];
int n, m, fa[N][21], deg[N];
int LCA(int x, int y) {
if (deg[x] < deg[y]) swap(x, y);
for (int i = 20; i >= 0; i--)
if (deg[fa[x][i]] >= deg[y]) x = fa[x][i];
if (x == y) return x;
for (int i = 20; i >= 0; i--)
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
int main() {
scanf("%s", s + 1); n = strlen(s + 1);
for (int i = 2, j = 0; i <= n; i++) {
while (j && s[i] != s[j + 1]) j = fa[j][0];
if (s[i] == s[j + 1]) j++;
fa[i][0] = j; for (int k = 1; k <= 20; k++) fa[i][k] = fa[fa[i][k - 1]][k - 1]; deg[i] = deg[fa[i][0]] + 1;
}
scanf("%d", &m);
while (m--) {
int x, y;
scanf("%d %d", &x, &y);
int ans = LCA(x, y);
if (ans == x || ans == y) ans = fa[ans][0];
printf("%d\n", ans);
}
return 0;
}
边栏推荐
- The next stop of data visualization platform | gifts from domestic open source data visualization datart "super iron powder"
- Matlab各函数说明
- 读写锁、共享锁、独占锁
- DSP development, using CCS software to establish engineering and burning
- Foreign lead operation takes one month to collect money, and the sideline still needs it
- Tang Yudi opencv background modeling
- Replace the function of pow with two-dimensional array (solve the time overrun caused by POW)
- Ansible 常用模块介绍
- Aruba学习笔记06-无线控制AC基础配置(CLI)
- TT ecosystem - cross border in-depth selection
猜你喜欢

Interviewer: man, how much do you know about the read-write lock of go language?

Pulse netizens have a go interview question, can you answer it correctly?

Six pictures show you why TCP shakes three times?

科目1-2

Open source summer interview | learn with problems, Apache dolphin scheduler, Wang Fuzheng

【基于ROS的URDF练习实例】四轮机器人与摄像头的使用

Publish your own library on NPM

web安全入门-开源防火墙Pfsense安装配置

Assignment operator (geritilent software - Jiuye training)

Houdini notes
随机推荐
Publish your own library on NPM
gnuplot软件学习笔记
Why does TCP shake hands three times instead of two times (positive version)
[the first anniversary of my creation] love needs to be commemorated, so does creation
链表——19. 删除链表的倒数第 N 个结点
Why is TCP a triple handshake
【翻译】使用gRPC和REST的微服务架构中的集成挑战
Read write lock, shared lock, exclusive lock
After watching the documentary "pirate treasure on Adak Island", I thought of the lost treasure in Chinese history
Problems and abuse of protocol buffers
Practice 4-6 number guessing game (15 points)
【我的创作一周年纪念日】爱情是需要被纪念的,创作也是
Interviewer: man, how much do you know about the read-write lock of go language?
03_ UE4 advanced_ illumination
How to use redis' publish subscribe (MQ) in.Netcore 3.1 project
Wildcards in MySQL like statements: percent, underscore, and escape
排序入门—插入排序和希尔排序
【基于ROS的URDF练习实例】四轮机器人与摄像头的使用
Protocol buffers 的问题和滥用
Definition and initialization of cv:: mat