Problem

BZOJ

Solution

可能是因为快要省选了,所以最近更博的频率好像高了点_(:зゝ∠)_

每个字符串最多有两个状态,然后要满足一些依赖关系,考虑2sat。可以先把字符串的结束节点在Trie树上建出来,这样它的前缀就是它的祖先,它作为前缀的就是它子树内的节点。利用Trie树的结构,建一棵向下的一棵向上的树优化连边,这样边数就减少到了 \(O(n)\) 的级别。

然而我高高兴兴写完之后连样例都过不去,调一调发现当两个字符串在同一个结束节点时也应该相互连边。为了避免边数退化,就把这些重复节点搞成链表的形式,复杂度就对了。但这样可能产生一个问题,因为链表是单向的,后来的一些点可能无法连向原来子树中的点,因此我们把所有字符串按长度排序,保证挂链表时结束节点的子树为空即可。

时间复杂度 \(O(n)\),但是带了 \(6\) 的常数。

Code

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
typedef long long ll;
const int maxn=3000010;
template <typename Tp> inline int getmin(Tp &x,Tp y){return y<x?x=y,1:0;}
template <typename Tp> inline int getmax(Tp &x,Tp y){return y>x?x=y,1:0;}
template <typename Tp> inline void read(Tp &x)
{
x=0;int f=0;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=1,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
if(f) x=-x;
}
struct data{int v,nxt;}edge[maxn<<1];
int n,p,tot,dfc,top,scc,head[maxn],len[maxn],rk[maxn],ch[maxn][2];
int dfn[maxn],low[maxn],stk[maxn],in[maxn],bel[maxn];
char str[500010];
string s[500010];
int A(int x){return x<<1;}
int B(int x){return x<<1|1;}
int cmp(const int &x,const int &y){return len[x]<len[y];}
void insert(int u,int v)
{
edge[++p]=(data){v,head[u]};head[u]=p;
u^=1;v^=1;
edge[++p]=(data){u,head[v]};head[v]=p;
}
void ins(int id,int x)
{
int rt=n+1,lst;
for(int i=0;i<len[id];i++)
{
if(!ch[rt][s[id][i]-'0'])
{
ch[rt][s[id][i]-'0']=++tot;
insert(B(rt),B(tot));
}
lst=rt;rt=ch[rt][s[id][i]-'0'];
}
++tot;
insert(B(rt),B(tot));
insert(x,B(tot));
insert(x,A(rt));
ch[lst][0]==rt?ch[lst][0]=tot:ch[lst][1]=tot;
}
void tarjan(int x)
{
dfn[x]=low[x]=++dfc;stk[++top]=x;in[x]=1;
for(int i=head[x];i;i=edge[i].nxt)
{
if(!dfn[edge[i].v]){tarjan(edge[i].v);getmin(low[x],low[edge[i].v]);}
else if(in[edge[i].v]) getmin(low[x],dfn[edge[i].v]);
}
if(dfn[x]==low[x])
{
int tmp;++scc;
do{
tmp=stk[top--];
in[tmp]=0;
bel[tmp]=scc;
}while(tmp^x);
}
}
void input()
{
read(n);tot=n+1;
for(int i=1;i<=n;i++)
{
scanf("%s",str);
s[i]=str;rk[i]=i;
len[i]=s[i].length();
}
sort(rk+1,rk+n+1,cmp);
for(int i=1;i<=n;i++)
{
int j=rk[i],pos=-1;
for(int r=0;r<len[j];r++)
if(s[j][r]=='?')
{
pos=j;
s[j][r]='0';ins(j,A(j));
s[j][r]='1';ins(j,B(j));
s[j][r]='?';
break;
}
if(pos==-1)
{
ins(j,A(j));
insert(B(j),A(j));
}
}
}
int main()
{
input();
for(int i=2,lim=B(tot);i<=lim;i++) if(!dfn[i]) tarjan(i);
for(int i=1;i<=tot;i++) if(bel[A(i)]==bel[B(i)]){puts("NO");return 0;}
puts("YES");
for(int i=1;i<=n;i++)
{
for(int j=0;j<len[i];j++)
if(s[i][j]=='?')
{
s[i][j]=(bel[A(i)]<bel[B(i)]?'0':'1');
break;
}
cout<<s[i]<<endl;
}
return 0;
}

BZOJ4840 NEERC2016 Binary Code的更多相关文章

  1. @gym - 101190B@ Binary Code

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 我们称一组字符串是 "前缀码",当且仅当不存 ...

  2. 格雷码(Gray Code)转二进制码(Binary Code)

    学习verilog generate语句时,偶然看到用generate语句来进行格雷码到二进制码转换的代码,就从网上找了一些案例来学习. 下表为几种自然二进制码与格雷码的对照表: 十进制数 自然二进制 ...

  3. USACO Party Lamps 【Binary code solvution】【规律】

    写这道题目的时候遇到了一个令人诧异的问题,就是平台上跑来的结果和我本机跑起来的结果不一样. 后来Debug了之后才发现是我数组开小了,只开到100 的数组竟然都去访问他170位的地址肯定要跪成翔啊.. ...

  4. Adaptive Code Via C#读书笔记

    原书链接: http://www.amazon.com/Adaptive-Code-via-principles-Developer-ebook/dp/B00OCLLYTY/ref=dp_kinw_s ...

  5. [LeetCode]题解(python):089 Gray Code

    题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...

  6. An update on OS X Code Signing(OS X代码签名)

    There has recently been updates to the OS X code signing process. These updates also affect Qt appli ...

  7. (DP 雷格码)Gray code -- hdu -- 5375

    http://acm.hdu.edu.cn/showproblem.php?pid=5375 Gray code Time Limit: 2000/1000 MS (Java/Others)    M ...

  8. Ascii vs. Binary Files

    Ascii vs. Binary Files Introduction Most people classify files in two categories: binary files and A ...

  9. Top 40 Static Code Analysis Tools

    https://www.softwaretestinghelp.com/tools/top-40-static-code-analysis-tools/ In this article, I have ...

随机推荐

  1. Trailing Zeroes (I) LightOJ - 1028(求因子个数)

    题意: 给出一个N 求N有多少个别的进制的数有后导零 解析: 对于一个别的进制的数要转化为10进制 (我们暂且只分析二进制就好啦) An * 2^(n-1) + An-1 * 2^(n-2) + `` ...

  2. BZOJ2878 [Noi2012]迷失游乐园 【基环树 + 树形dp + 期望dp】

    题目链接 BZOJ2878 题解 除了实现起来比较长,思维难度还是挺小的 观察数据范围发现环长不超过\(20\),而我们去掉环上任何一个点就可以形成森林 于是乎我们枚举断掉的点,然后只需求出剩余每个点 ...

  3. 洛谷 P3171 [CQOI2015]网络吞吐量 解题报告

    P3171 [CQOI2015]网络吞吐量 题目描述 路由是指通过计算机网络把信息从源地址传输到目的地址的活动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为路由器.为了使数据包最 ...

  4. centos_radhat升级系统

    Centos/Redhat系统下,Yum升级操作系统版本方法: 很多情况下,系统安装的时候是一个版本,官方很快就会出新的版本,提高系统的安全性,以及一些漏洞补丁 通过以下步骤可以平滑将系统升级,例如C ...

  5. 如何使用android design support library

    Android应用Design Support Library完全使用实例 - OPEN 开发经验库http://www.open-open.com/lib/view/open143338585611 ...

  6. bzoj 3224

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 16656  Solved: 7255[Submit][St ...

  7. tp5.1 redis 使用

    第一步:在框架根目录config里面新建redis.php文件配置ip及端口:如下: <?php return [ 'host' => '140.143.190.248', 'port' ...

  8. Java泛型底层源码解析-ArrayList,LinkedList,HashSet和HashMap

    声明:以下源代码使用的都是基于JDK1.8_112版本 1. ArrayList源码解析 <1. 集合中存放的依然是对象的引用而不是对象本身,且无法放置原生数据类型,我们需要使用原生数据类型的包 ...

  9. codevs 3369 膜拜(线型)

    3369 膜拜 http://codevs.cn/problem/3369/ 题目描述 Description 神牛有很多…当然…每个同学都有自己衷心膜拜的神牛.某学校有两位神牛,神牛甲和神牛乙.新入 ...

  10. [洛谷P4491] [HAOI2018]染色

    洛谷题目链接:[HAOI2018]染色 题目背景 HAOI2018 Round2 第二题 题目描述 为了报答小 C 的苹果, 小 G 打算送给热爱美术的小 C 一块画布, 这块画布可 以抽象为一个长度 ...