当前位置:网站首页>[COCI] lattice (dichotomy + tree divide and conquer + string hash)
[COCI] lattice (dichotomy + tree divide and conquer + string hash)
2022-04-23 09:43:00 【Zimba_】
The question :
Given a tree n n n A tree of nodes , Each node has a lowercase letter a — z a—z a—z. Require the longest simple path , The string obtained along the path is a palindrome string .
( 1 ≤ n ≤ 1 0 5 1\leq n \leq10^{5} 1≤n≤105)
Ideas :
The first thing to consider is , How to judge whether a path on the tree is a palindrome string .
The method used here is String hash .
for instance , to a — z a—z a—z Each letter has its own ( My life depend on myself not the fate ) Set a hash value , Set up another b a s e base base Value and modulus M M M, character string a b b c b b a abbcbba abbcbba The hash value of is H ( a ) + H ( b ) × b a s e + H ( b ) × b a s e 2 + H ( c ) × b a s e 3 + … + H ( a ) × b a s e 6 ( m o d M ) H(a)+H(b)\times base+H(b)\times base^{2}+H(c)\times base^{3}+…+H(a)\times base^{6} (mod\;M) H(a)+H(b)×base+H(b)×base2+H(c)×base3+…+H(a)×base6(modM).
that , After processing the prefix hash value , You can quickly calculate the hash value of the substring .
Determine whether a string is a palindrome string , We just need to get the hash value from the front to the back and the hash value from the back to the front , Compare whether they are the same .
So many times to ask whether a certain paragraph string is a palindrome string , We just preprocess the hash prefix from the front to the back , And the hash suffix from back to front . You can quickly calculate the hash values of a certain section from front to back and back to front for comparison , To determine whether it is a palindrome string .
With this tool , For this problem, we just need to preprocess the hash value on the tree , You can determine whether the path is a palindrome string through calculation .
Now? , Let's think about how to find the longest palindrome path . This is obviously satisfying Two points nature . therefore , The length of our binary path , Then determine whether there is a palindrome path of this length .
that , Now there is only one last question , Is how to check whether there is a palindrome path of a certain length .
Let's set the root to R R R, So there are two paths , One is through R R R Of , One is not through R R R Of .
Suppose we can o ( n ) o(n) o(n) Calculate the course R R R The situation of , For those who do not go through R R R The situation of , Paths are in its subtree . So we can use Center of gravity decomposition , That is to say The trees divide To solve the , Just take the center of gravity as R R R, The overall complexity is o ( n l o g n ) o(nlogn) o(nlogn).
therefore , The last problem we need to solve now is , how o ( n ) o(n) o(n) Check for the presence of tree roots R R R, And the palindrome path with a given length .
For a passage R R R The path of , It must look like this .( Solve the problem of stealing pictures )

Determine whether this path is a palindrome string , We just have to judge R R R To C C C It's a palindrome string , also C C C To A A A String sum of R R R To C C C The same string .
We enumerate A A A Settle down , Now C C C It can also be calculated , We have judged R C RC RC, Work out C A CA CA, Just look in other subtrees , Whether there is R B RB RB The value of and C A CA CA identical . We put the other subtrees R B RB RB Value saved to s e t set set in , Can o ( l o g n ) o(logn) o(logn) look for .
When we use u n o r d e r e d _ s e t unordered\_set unordered_set When , It becomes o ( 1 ) o(1) o(1) Look for it. . So the complexity becomes o ( n ) o(n) o(n) 了 .
Last , The overall complexity is o ( n l o g 2 n ) o(nlog^{2}n) o(nlog2n) 了 .
Code :
COO for a while .
版权声明
本文为[Zimba_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230621424994.html
边栏推荐
- Cloud identity is too loose, opening the door for attackers
- 面试官:说几个PHP常用函数,幸好我面试之前看到了这篇文章
- Chinese Remainder Theorem and extended Chinese remainder theorem that can be understood by Aunt Baojie
- SAP 101K 411k inventory change
- Vivo, hardware safe love and thunder
- Enter "net start MySQL" and "system error 5. Access denied" appears. Detailed explanation of the problem
- DVWA range practice
- 【读书笔记】《Verilog数字系统设计教程》 第5章 条件语句、循环语句和块语句(附思考题答案)
- Leetcode0587. Install fence
- [educational codeforces round 80] problem solving Report
猜你喜欢

Enter "net start MySQL" and "system error 5. Access denied" appears. Detailed explanation of the problem

kernel-pwn学习(3)--ret2user&&kernel ROP&&QWB2018-core

Random neurons and random depth of dropout Technology

AI上推荐 之 MMOE(多任务yyds)

Yyds dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '

DVWA range practice record

Practice of Flink streaming batch integration in Xiaomi

Easy to understand subset DP

Introduction to sap pi / PO login and basic functions

653. Sum of two IV - input BST
随机推荐
AI上推荐 之 MMOE(多任务yyds)
SAP excel has completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.
Redis 内存占满导致的 Setnx 命令执行失败
SAP 101K 411k inventory change
PHP笔记(一):开发环境配置
Kettle experiment (III)
How to use SQL statement union to get another column of another table when the content of a column in a table is empty
Go language learning notes - slice, map | go language from scratch
Sql1 [geek challenge 2019]
Kettle实验 转换案例
面试官:说几个PHP常用函数,幸好我面试之前看到了这篇文章
Compile and debug mysql8 with clion under MacOS x
Kettle实验 (三)
MySQL of database -- basic common query commands
STM32 and FreeRTOS stack parsing
Give the method of instantiating the object to the new object
Introduction to sap pi / PO login and basic functions
Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
Employee probation application (Luzhou Laojiao)
JS what is an event? Event three elements and operation elements