Codeforces Beta Round #37 C. Old Berland Language 暴力 dfs
C. Old Berland Language
题目连接:
http://www.codeforces.com/contest/37/problem/C
Description
Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol.
Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves.
Input
The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000.
Output
If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any.
Sample Input
3
1 2 3
Sample Output
YES
0
10
110
Hint
题意
有n个串,并且每个串的长度为p[i]
你需要构造出这n个串,并且这n个串互相不为其他串的前缀
问你是否能够构造出来
不能输出no
题解:
直接dfs一波,就莽过去了……
复杂度很显然是最多往下面搜1000层吧……
感觉分支不是很多的样子
233
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int n;
struct node
{
int x,y;
string ans;
}p[maxn];
int now,ok;
bool cmp(node a,node b)
{
return a.x<b.x;
}
bool cmp2(node a,node b)
{
return a.y<b.y;
}
void dfs(int x,string s)
{
if(x==p[now].x)
{
p[now++].ans=s;
if(now==n)ok=1;
return;
}
dfs(x+1,s+'0');
if(now==n)return;
dfs(x+1,s+'1');
if(now==n)return;
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&p[i].x);
p[i].y=i;
p[i].ans="";
}
sort(p,p+n,cmp);
dfs(0,"");
if(!ok)printf("NO\n");
else{
printf("YES\n");
sort(p,p+n,cmp2);
for(int i=0;i<n;i++)
cout<<p[i].ans<<endl;
}
}
Codeforces Beta Round #37 C. Old Berland Language 暴力 dfs的更多相关文章
- Codeforces Beta Round #37 B. Computer Game 暴力 贪心
B. Computer Game 题目连接: http://www.codeforces.com/contest/37/problem/B Description Vasya's elder brot ...
- Codeforces Beta Round #37 A. Towers 水题
A. Towers 题目连接: http://www.codeforces.com/contest/37/problem/A Description Little Vasya has received ...
- Codeforces Beta Round #1 C. Ancient Berland Circus 计算几何
C. Ancient Berland Circus 题目连接: http://www.codeforces.com/contest/1/problem/C Description Nowadays a ...
- Codeforces Beta Round #8 B. Obsession with Robots 暴力
B. Obsession with Robots 题目连接: http://www.codeforces.com/contest/8/problem/B Description The whole w ...
- Codeforces Beta Round #87 (Div. 2 Only)-Party(DFS找树的深度)
A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exa ...
- Codeforces Beta Round #22 (Div. 2 Only) E. Scheme dfs贪心
E. Scheme To learn as soon as possible the latest news about their favourite fundamentally new ope ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- Codeforces Beta Round #5 B. Center Alignment 模拟题
B. Center Alignment 题目连接: http://www.codeforces.com/contest/5/problem/B Description Almost every tex ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
随机推荐
- 【原创】Linux环境下的图形系统和AMD R600显卡编程(1)——Linux环境下的图形系统简介
Linux/Unix环境下最早的图形系统是Xorg图形系统,Xorg图形系统通过扩展的方式以适应显卡和桌面图形发展的需要,然而随着软硬件的发展,特别是嵌入式系统的发展,Xorg显得庞大而落后.开源社区 ...
- 大数据系列之分布式大数据查询引擎Presto
关于presto部署及详细介绍请参考官方链接 http://prestodb-china.com PRESTO是什么? Presto是一个开源的分布式SQL查询引擎,适用于交互式分析查询,数据量支持G ...
- Python之容器类Collections
容器类Collections 标签(空格分隔): Python进阶 defaultdict counter deque namedtuple defaultdict defaultdict的作用是可以 ...
- 使用UDP和TCP协议的各种应用和应用层协议
IGMP和ICMP是传输层协议
- docker centos:latest 使用 sshd
一.术语 1.容器 很多用户在接触Docker 之初都会认为容器就是一种轻量级的虚拟机,但实际上,容器和虚拟机有非常大的区别.从根本形态上来看,容器其实就是运行在操作系统上的一个进程,只不过加入了对资 ...
- GreenPlum学习笔记:新手入门命令
1.命令行登录数据库 psql -h 192.168.111.111 -U username -d dbname 其中,username为数据库用户名,dbname为数据库名,执行后提示输入密码.(可 ...
- 使用mongo-java-driver-3.0.2连接MongoDB数据库
这里使用的mongodb的java驱动版本是:3.0.2,文件名mongo-java-driver-3.0.2.jar 博客本地下载下载网址(也可以下载其它版本):http://central.ma ...
- centos killall安装
https://blog.csdn.net/joeyon1985/article/details/46707865 https://blog.csdn.net/xupeng874395012/arti ...
- java jar 自启动 centos7 systemctl
我本地有一个 data-service.jar 1. 编写启动脚本 data-service-start [root@iz2ze0fq2isg8vphkpos5sz shell]# more dat ...
- day1作业登录接口总结
作业一:编写登陆接口 1.输入用户名和密码 2.认证成功后显示欢迎信息 3.输错三次后锁定 上面作业,用了几种思路来解决问题:但是本质上其实都是一样的:核心都是对文件的操作,文件的增删改查:并且这些操 ...