Binary_Code 2-SAT神题

本文介绍了如何利用Trie树优化2-SAT建图方法来解决一个关于二进制前缀码的问题。文章通过具体的例子说明了在遇到字符串前缀后缀相关题目时,如何利用Trie树优化建图策略,并提到了在2-SAT判断过程中的一些关键技巧,如严格子树限制和前缀优化建边。最后,文章强调了清晰思维和长变量名在解题中的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem B. Binary Code
Input file: binary.in
Output file: binary.out
Ben has recently learned about binary prefix codes. A binary code is a set of n distinct nonempty code
words si
, each consisting of 0s and 1s. A code is called a prefix code if for every i 6= j neither si
is a prefix
of sj nor sj is a prefix of si
. A word x is called a prefix of a word w if there exists a possibly empty word
y, such that xy = w. For example, x = 11 is a prefix of w = 110 and x = 0100 is a prefix of w = 0100.
Ben found a paper with n lines of binary code in it. However, this paper is pretty old and there are some
unreadable characters. Fortunately, each word contains at most one unreadable character.
Ben wants to know whether these n lines could represent a binary prefix code. In other words, can he
replace every unreadable character with 0 or 1, so that the code becomes a prefix code?
Input
The first line contains integer n — the number of code words (1 ≤ n ≤ 5 · 105
).
Next n lines contain nonempty code word records, one per line. Each code word record consists of “0”,
“1” and “?” characters. Every code word record contains at most one “?” character that represents
unreadable character.
The total length of words does not exceed 5 · 105
.
Output
Output “NO” in the first line if the code cannot be a prefix code.
Otherwise, output “YES” in the first line. Next n lines shall contain code words in the same order as the
corresponding code word records in the input.
If there are several prefix codes, that could have been written on the paper, output any one.
Examples
binary.in binary.out
4
00?
0?00
?1
1?0
YES
000
0100
11
100
3
0100
01?0
01?0
NO

http://codeforces.com/gym/101190/attachments/download/4956/20162017-acmicpc-northeastern-european-regional-contest-neerc-16-en.pdf
题面.
早就听orbitingflea大佬说起此题了,今天终于将其AC了.

trie树优化2-SAT建图:

Trie树优化建图的精髓是利用原图前缀或后缀的性质,利用节点与子树的关系优化建图.遇到这类字符串前缀后缀以及图论相关的题可以往Trie树这一方面想

trie树上往x的1连边往其父亲的0连边这个限制一开始没想到,Trie图优化建边要满足严格子树2-SAT的trick.

第二个trick,在判断两个相同串时需要判断一个集合内最多有一个元素出现一次的Trick,其trick是新建前缀优化建边.当前点与其对应前一个的前缀互斥,前缀维护前缀or,然后当前点能推出当前前缀点.这样能O(n)满足条件.2-SAT建边Trick

第三个Trick是orbitingflea大佬所说的2-SAT输出方案用tarjan的话对于每个点直接选择scc_no小的那个就能保证正确性,具体证明见orbitingflea博客.

发现理清思路加长变量名思维会清晰很多,以后做题也要这样.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值