当前位置:网站首页>[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
边栏推荐
- Canary publishing using ingress
- 《信息系统项目管理师总结》第八章 项目干系人管理
- How to obtain geographical location based on photos and how to prevent photos from leaking geographical location
- 112. Path sum
- SAP 03-amdp CDs table function using 'with' clause
- Go language learning notes - array | go language from scratch
- Yyds dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '
- Personal homepage software fenrus
- Codeforces Round #784 (Div. 4)
- 论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——3背景
猜你喜欢
Kettle experiment (III)
Two methods of building Yum source warehouse locally
What is monitoring intelligent playback and how to use intelligent playback to query video recording
Canary publishing using ingress
Dropout技术之随机神经元与随机深度
DVWA range practice
PHP笔记(一):开发环境配置
kernel-pwn学习(3)--ret2user&&kernel ROP&&QWB2018-core
High paid programmer & interview question series 91 limit 20000 loading is very slow. How to solve it? How to locate slow SQL?
[geek challenge 2019] havefun1
随机推荐
Leetcode0587. Install fence
Unfortunately, I broke the leader's confidential documents and spit blood to share the code skills of backup files
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》
Exclusive thoughts and cases of JS
JS DOM event
Kettle experiment (III)
Number of islands
MySQL of database -- Fundamentals
DVWA range practice record
JS scope, scope chain, global variables and local variables
RSA encryption and decryption signature verification
ASUS laptop can't read USB and surf the Internet after reinstalling the system
Es aggregation aggregation analysis
NPM reports an error: operation not allowed, MKDIR 'C: \ program files \ node JS \ node_ cache _ cacache’
Image processing in opencv -- Introduction to contour + contour features
ALV tree (ll LR RL RR) insert delete
Flink 流批一体在小米的实践
Golang force buckle leetcode 396 Rotation function
SAP pi / PO soap2proxy consumption external WS example
501. Mode in binary search tree