当前位置:网站首页>[Luogu p3426] SZA template (string) (KMP)
[Luogu p3426] SZA template (string) (KMP)
2022-07-24 09:12:00 【SSL_ TJH】
SZA-Template
Topic link :luogu P3426
The main idea of the topic
Give you a string , Then you can choose a string to generate it .
Each time you can put a string of your choice onto the current string , Then if the following paragraph is the same as the previous paragraph , Then you can overlap .
( That is, a character in one position can appear many times , But different characters cannot appear )
Then how short is the shortest string you can use .
Ideas
First of all, an obvious thing is that the satisfied string is at least string Border.
Then consider what one Border Can only be , That's it fail From the tree n n n The adjacent points on the path of must be able to be found .
As for how to see if it can be made up , That is the current string, which goes to the next string , It must be at least as long as Xinduo ( That is, you can get a new one by two ), As for why this is Border Definition , Just think about it .
What do you think , We consider the dfs, Every time I come to Border Click to delete other brothers , Then the linked list maintains the middle distance , Then delete yourself and update the gap size every time you go to your son , That is, the gap size should be less than or equal to the length of the point you want to see .
Code
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int N = 5e5 + 100;
char s[N];
int n, l[N], r[N], fail[N], ans, gap;
bool sp[N];
vector <int> G[N];
void del(int now) {
r[l[now]] = r[now];
l[r[now]] = l[now];
}
void dfs(int now) {
if (sp[now] && now >= gap) {
ans = min(ans, now);
}
for (int i = 0; i < G[now].size(); i++) {
int x = G[now][i];
if (sp[x]) continue;
dfs(x);
}
if (l[now] && r[now] != n + 1) {
gap = max(gap, r[now] - l[now]);
}
del(now);
for (int i = 0; i < G[now].size(); i++) {
int x = G[now][i];
if (!sp[x]) continue;
dfs(x);
}
}
int main() {
scanf("%s", s + 1); n = strlen(s + 1);
ans = n;
G[0].push_back(1);
for (int i = 2, j = 0; i <= n; i++) {
while (j && s[i] != s[j + 1]) j = fail[j];
if (s[i] == s[j + 1]) j++; fail[i] = j; G[j].push_back(i);
}
int now = n; while (now) sp[now] = 1, now = fail[now];
for (int i = 1; i <= n; i++) l[i] = i - 1, r[i] = i + 1;
gap = 1;
dfs(0);
printf("%d", ans);
return 0;
}
边栏推荐
- We were tossed all night by a Kong performance bug
- Run little turtle to test whether the ROS environment in the virtual machine is complete
- Gnuplot software learning notes
- Re6:读论文 LeSICiN: A Heterogeneous Graph-based Approach for Automatic Legal Statute Identification fro
- One year after I came to Ali, I ushered in my first job change
- First acquaintance with JVM
- JS problem summary
- Vector control of permanent magnet synchronous motor (I) -- mathematical model
- Account 1-3
- CUDA day 2: GPU core and Sm core components [easy to understand]
猜你喜欢

【汇编语言实战】一元二次方程ax2+bx+c=0求解(含源码与过程截屏,可修改参数)

How to integrate and use log4net logging plug-in in vs2019 class library

Super complete summary: how to operate files in go language

来阿里一年后我迎来了第一次工作变动....

Redis learning - Introduction to redis and NiO principles

Account 1-3

Why does TCP shake hands three times instead of two times (positive version)

链表——19. 删除链表的倒数第 N 个结点

C language practice questions + Answers:

Asyncdata cross domain error after nuxt route switching
随机推荐
Un7.22: how to upload videos and pictures simultaneously with the ruoyi framework in idea and vs Code?
【我的创作一周年纪念日】爱情是需要被纪念的,创作也是
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
UE5影视动画渲染MRQ分层学习笔记
利用opencv 做一个简单的人脸识别
Code random notes_ Linked list_ Turn over the linked list in groups of 25K
Tiktok video traffic golden release time
How to judge and analyze NFT market briefly through NFT go?
Pulse netizens have a go interview question, can you answer it correctly?
Detailed explanation of the whole process of R & D demand splitting | agile practice
How to open the port number of the server, and the corresponding port of common network services
One year after I came to Ali, I ushered in my first job change
CUDA day 2: GPU core and Sm core components [easy to understand]
[FFH] openharmony gnawing paper growth plan -- Application of cjson in traditional c/s model
JUC强大的辅助类
Map processing background management menu data
Tiktok's "online celebrity" was poached by Amazon and broadcast on Amazon live platform
Advantages of using partitions
Houdini official HDA sidefx labs installation
[assembly language practice] solve the unary quadratic equation ax2+bx+c=0 (including source code and process screenshots, parameters can be modified)