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 现在你已经有一 ...
随机推荐
- Python标准库笔记(9) — functools模块
functools 作用于函数的函数 functools 模块提供用于调整或扩展函数和其他可调用对象的工具,而无需完全重写它们. 装饰器 partial 类是 functools 模块提供的主要工具, ...
- go 数组
数组的定义和 初始化 数组是同一类型的元素集合 ]int //定义⼀个数组 Go中数组下标从0开始,因此长度为n的数组下标范围:[0,n-1] 整数数组中的元素默认初始化为0,字符串数组中的元素默认初 ...
- 一步一步搭建 oracle 11gR2 rac+dg之grid安装(四)【转】
一步一步在RHEL6.5+VMware Workstation 10上搭建 oracle 11gR2 rac + dg 之grid安装 (四) 转自 一步一步搭建 oracle 11gR2 rac+d ...
- axios通过django的csrf验证
django会在浏览器的cookie里面保存一项csrftoken=GvzB3ilhlgadishmascacsilreclherlkjhaklsdv3qx4M96XRG88omScDPQaKoMxJ ...
- shell用户管理->
用户的添加与删除练习 -> 脚本1(if then) 思路:1.条件测试, 脚本使用案例, 创建用户[交互式创建] 1.怎么交互式 read -p 2.接收到对应字符串怎么创建用户 userad ...
- 20 Organizing Go code 组织go代码
Organizing Go code 16 August 2012 Introduction Go code is organized differently to that of other lan ...
- 利用CSS函数calc(...)实现Web页面左右布局
前言 因为自己的网站需要,想要做一个左右布局的页面: 左边是导航菜单之类的东西.右边是文档内容(因为最近看的一些软件的文档页面都是这么布局的): 左边固定宽度——300像素.右边使用剩余的宽度: 左边 ...
- 数据科学实战手册(R+Python)书中引用资料网址
本文会持续将<数据科学实战手册(R+Python)>一书中的附带参考资料网址手打出来, 方便访问. 由于书中的参考资料网址太多, 这个文档将可能花费一段时间才能完成. 第一章 P7 Rs ...
- python+selenium第一步 - 环境搭建
刚开始学习一门技术,肯定是要从环境搭建开始的,我也不例外. 首先选择需要安装的版本,我使用的是mac自带的2.7版本. selenium2,和火狐浏览器 为求稳定不会出现未知问题,我选择了seleni ...
- sizeof求结构体大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...