当前位置:网站首页>[uoj 429] runs (inclusive) + a little record about Lyndon tree and its application
[uoj 429] runs (inclusive) + a little record about Lyndon tree and its application
2022-07-26 04:29:00 【SSL_ TJH】
String Division
Topic link :UOJ 429
The main idea of the topic
Give you a string , Then ask how many divisions you have , Satisfy that two adjacent strings are not equal , And each string does not have a whole period .
Ideas
Lyndon Tree
L y n d o n T r e e \rm Lyndon\ Tree Lyndon Tree What is it? ?
It's that you can't put two strings L y \rm Ly Ly Put it together to get a new L y \rm Ly Ly Do you , Let's find the smallest suffix for a string ( It can't count itself ), Then get two strings , Then keep dividing , Got it. L y n d o n T r e e \rm Lyndon\ Tree Lyndon Tree.
Then this thing is built on this string is L y \rm Ly Ly On the basis of , If not, it is for L y \rm Ly Ly Do this for each string divided , Form a forest .
Then when you think about it, you will find that it is S A \rm SA SA Of r a n k \rm rank rank Array to build a Cartesian tree to get the tree .
Then there are some properties :
If [ l , r ] [l,r] [l,r] yes L y \rm Ly Ly, that l , r l,r l,r The character corresponds to the point on the tree L C A \rm LCA LCA yes a a a( set up ), For substrings [ l a , r a ] [l_a,r_a] [la,ra], There will be l = l a ⩽ r a ⩽ r l=l_a\leqslant r_a\leqslant r l=la⩽ra⩽r.
Then if this interval is L y s , f ( l ) \rm {Ly}_{s,f}(l) Lys,f(l)( It's not difficult to feel that this tree is also divided f f f Of ), It must be on the right son ( Because it is the longest L y \rm Ly Ly ah )
Then it's almost the same , Any one r u n \rm run run Of L y R \rm LyR LyR It must appear in the right son ( This is right here f ∈ { 0 , 1 } f\in\{0,1\} f∈{ 0,1} One of them ), Then it seems to see that satisfaction s r + 1 < f s r + 1 − p s_{r+1}<_fs_{r+1-p} sr+1<fsr+1−p
It seems that I haven't said it before W P L \rm WPL WPL and P L \rm PL PL, Here is a sentence .
For a string S S S Yes p , q p,q p,q Two cycles , If p + q ⩽ ∣ S ∣ p+q\leqslant |S| p+q⩽∣S∣, Then there are gcd ( p , q ) \gcd(p,q) gcd(p,q) The cycle of ( W P L \rm WPL WPL, W e a k P e r i o d i c i t y L e m m a \rm Weak\ Periodicity\ Lemma Weak Periodicity Lemma)
( You can move a position by two cycles to get the minus position , And then divide them )
Then it can actually be extended to p + q − gcd ( p , q ) ⩽ ∣ S ∣ p+q-\gcd(p,q)\leqslant |S| p+q−gcd(p,q)⩽∣S∣, Namely P L \rm PL PL, P e r i o d i c i t y L e m m a \rm Periodicity Lemma PeriodicityLemma 了 .
( I don't know , It seems that the upper bound can be obtained by giving an example first , Then proportion Bala makes a connection )
What's the use , One can be called “ Substring semi periodic query ” Things that are (Two-Period Queries)
This thing requires you to quickly query whether a substring has a period of no more than half its length , If there is one with the smallest output .
Let's define a e x r u n ( i , j ) {\rm exrun}(i,j) exrun(i,j) To satisfy i ′ ⩽ i , j ⩽ j ′ , p ⩽ ( j − i + 1 ) / 2 i'\leqslant i,j\leqslant j',p\leqslant(j-i+1)/2 i′⩽i,j⩽j′,p⩽(j−i+1)/2 One of the r u n ( i ′ , j ′ , p ) \rm run(i',j',p) run(i′,j′,p).
Then we can prove that it is unique if it exists .
( Because if it's not the only , Those that overlap can be used W P L \rm WPL WPL Find contradictions )
Then this definition can solve the above problem , Let's think about it f ∈ { 0 , 1 } f\in\{0,1\} f∈{ 0,1} All built L y n d o n T r e e \rm{Lyndon\ Tree} Lyndon Tree, And then let a = l c a ( [ i , ⌈ ( i + j ) / 2 ⌉ ] ) a={\rm lca}([i,\left\lceil(i+j)/2\right\rceil]) a=lca([i,⌈(i+j)/2⌉]), Then judge whether the right son meets the conditions .
As for this position, it can be judged because there must be a corresponding L y R \rm LyR LyR contain ⌈ ( i + j ) / 2 ⌉ \left\lceil(i+j)/2\right\rceil ⌈(i+j)/2⌉, According to the above properties , It will appear in the position of the right son at a certain point .
And then we'll find out a a a Location will also > p >p >p, It also includes that location , therefore a a a It would be this L y R \rm LyR LyR The ancestors of the .
As long as its right son is not that L y R \rm LyR LyR, It will also be its ancestors .
That's because the right son can get bigger positions in turn .
If the one in the middle ⩽ j \leqslant j ⩽j, Then its corresponding position is p p p This cycle , Then it's not L y \rm Ly Ly 了 , contradiction .
If > j >j >j, that L y R \rm LyR LyR The interval formed by its left boundary and its right boundary will be smaller than the interval formed by it , That is also related to it L y \rm Ly Ly There's a contradiction ( By definition ).
So the opposite is true .
This question
This question is still very friendly !
( At least not a tree array )
Consider how to find illegal , When you see the division, consider giving each divided string a function , Then the weight of a division is all multiplied ( In this way, illegal ones will be directly given 0 0 0)
Then I found that if there is no whole cycle, I can do , But it is said that the two cannot be the same .
Considering that the same is very similar , If you combine two identical ones, the whole cycle ahead will be formed .
So you consider letting go , The situation of the whole cycle and the offset between the two .
Then think about it. It is not difficult to find that the tolerance exclusion coefficient is the number of occurrences of the minimum cyclic section of this string .
Then consider DP, It's not hard to get n 2 n^2 n2 In the form of ( Enumerate where the new substring starts ), Consider optimizing .
The first special thing is that there is a whole cycle , Then we can prefix and , Then subtract the one with the whole period .
Then consider the whole cycle , First turn on the r u n \rm run run Find out , Then we can get some intervals .
Found that for a r u n \rm run run, r r r It can provide an isochronous sequence where a point can be transferred from .
Then we only need for each r r r Maintain the contribution and sum of the points of each arithmetic sequence that currently appears .
Then notice a point , The number of cycles of other previous points + 1 +1 +1( Or add one to all , The last one is from 0 0 0 To 1 1 1), Then you can take it in reverse and add it .
Code
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#define ll long long
#define mo 998244353
#define ull unsigned long long
using namespace std;
const int N = 2e5 + 100;
char s[N];
int n, ly[N], m;
struct RUNS {
int x, y, p;
}runs[N << 1];
vector <int> ed[N];
vector <ll> sum[N << 1][2];
//0: Subtract the prefix and
//1: A class
ll f[N];
struct HASH {
const int di = 131;
ull mi[N], hash[N];
void Init() {
mi[0] = 1;
for (int i = 1; i <= n; i++) {
mi[i] = mi[i - 1] * di;
hash[i] = hash[i - 1] * di + s[i] - 'a' + 1;
}
}
ull get(int x, int y) {
return hash[y] - hash[x - 1] * mi[y - x + 1];
}
}H;
int getl(int x, int y) {
int l = 1, r = min(x, y), re = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (H.get(x - mid + 1, x) == H.get(y - mid + 1, y)) re = mid, l = mid + 1;
else r = mid - 1;
}
return re;
}
int getr(int x, int y) {
int l = 1, r = min(n - x + 1, n - y + 1), re = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (H.get(x, x + mid - 1) == H.get(y, y + mid - 1)) re = mid, l = mid + 1;
else r = mid - 1;
}
return re;
}
int sta[N];
bool cmpS(int x, int y) {
int pl = getr(x, y);
return s[x + pl] < s[y + pl];
}
void Lyndon(bool op) {
sta[0] = 0; sta[++sta[0]] = n + 1; sta[++sta[0]] = n; ly[n] = n;
for (int i = n - 1; i >= 1; i--) {
while (sta[0] > 1 && cmpS(i, sta[sta[0]]) == op) sta[0]--;
sta[++sta[0]] = i; ly[i] = sta[sta[0] - 1] - 1;
}
}
void check(int x, int y) {
int l = getl(x, y), r = getr(x, y);
if (l + r >= y - x + 1) runs[++m] = (RUNS){
x - l + 1, y + r - 1, y - x};
}
bool cmp_runs(RUNS x, RUNS y) {
if (x.x != y.x) return x.x < y.x;
if (x.y != y.y) return x.y < y.y;
return x.p < y.p;
}
void Init() {
H.Init();
for (int op = 0; op <= 1; op++) {
Lyndon(op);
for (int i = 1; i <= n; i++) {
check(i, ly[i] + 1);
}
}
sort(runs + 1, runs + m + 1, cmp_runs);
int tmp = m; m = 0;
for (int i = 1; i <= tmp; i++)
if (runs[i].x != runs[i - 1].x || runs[i].y != runs[i - 1].y)
runs[++m] = runs[i];
}
int main() {
scanf("%s", s + 1); n = strlen(s + 1);
Init();
for (int i = 1; i <= m; i++) {
for (int r = runs[i].x + 2 * runs[i].p - 1; r <= runs[i].y; r++)
ed[r].push_back(i);
int sz = runs[i].p;
if (runs[i].y - (runs[i].x + 2 * runs[i].p - 1) + 1 <= runs[i].p)
sz = (runs[i].y - runs[i].x + 1) % runs[i].p;
sz++;
sum[i][0].resize(sz); sum[i][1].resize(sz);
}
f[0] = 1; ll Sum = 1;
for (int i = 1; i <= n; i++) {
f[i] = Sum;
for (int j = 0; j < ed[i].size(); j++) {
int v = ed[i][j];
int l = runs[v].x, r = runs[v].y, p = runs[v].p;
int id = (i - l + 1) % p;
(sum[v][0][id] += mo - f[i - 2 * p]) %= mo;
(sum[v][1][id] *= mo - 1) %= mo; (sum[v][1][id] += mo - f[i - 2 * p]) %= mo;
(f[i] += sum[v][0][id] + sum[v][1][id]) %= mo;
}
(Sum += f[i]) %= mo;
}
printf("%lld", f[n]);
return 0;
}
边栏推荐
- Comprehensive evaluation and decision-making method
- Spark Structured Streaming HelloWorld
- UE4 获取玩家控制权的两种方式
- dijango学习
- UE4 多个角色控制权的切换
- Compiled by egg serialize TS
- MySQL日志分类:错误日志、二进制日志、查询日志、慢查询日志
- ASP. Net core actionfilter filter details
- Life related -- the heartfelt words of a graduate tutor of Huake (mainly applicable to science and Engineering)
- Steam科学教育赋予课堂教学的创造力
猜你喜欢

SEGGER Embedded Studio找不到xxx.c或者xxx.h文件

Chapter 3 how to use sourcetree to submit code

MySQL - multi table query - Cartesian product sum, correct multi table query, equivalent connection and unequal connection, inner connection and outer connection

Phaser(一):平台跳跃收集游戏

Acwing_ 12. Find a specific solution for the knapsack problem_ dp

Authentication Encyclopedia (cookies, sessions, tokens, JWT, single sign on), in-depth understanding and understanding of authentication

Recommendation | DBT skills training manual: baby, you are the reason why you live

Steam科学教育赋予课堂教学的创造力

Yapi installation

数组排序1
随机推荐
Sangi diagram of machine learning (for user behavior analysis)
力扣每日一题-第42天-661. 图片平滑器
Weights & Biases (二)
makefile知识再整理(超详细)
A series of problems about the number of DP paths
1. Excel的IF函数
性能和成本的综合架构:单元化架构
支持代理直连Oracle数据库,JumpServer堡垒机v2.24.0发布
Unable to find sygwin.s file during vscode debugging
[C language foundation] 13 preprocessor
VM virtual machine has no un bridged host network adapter, unable to restore the default configuration
吴恩达机器学习课后习题——线性回归
The difference between positive samples, negative samples, simple samples and difficult samples in deep learning (simple and easy to understand)
十一、异常处理器
MySQL - multi table query - Cartesian product sum, correct multi table query, equivalent connection and unequal connection, inner connection and outer connection
MapReduce中分区数与ReduceTask个数关系比较
Yuansaka Lin wallpaper
Soft simulation rasterization renderer
吴恩达机器学习课后习题——逻辑回归
Comprehensive evaluation and decision-making method