Old Berland Language
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, 0and 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.
Example
3
1 2 3
YES
0
10
110
3
1 1 1
NO
咸鱼如我看到的第一瞬间是懵逼的。最后在题解的帮助下发现了做法π__π。这道题是用dfs查找出符合长度条件的字符串。菜鸡完全届不到。
#include<bits/stdc++.h> #define maxn 1010 typedef long long ll; using namespace std; int n,num=0; bool f=0; struct node{ int l; int m; string str;}; struct node a[maxn]; bool cmp(node a,node b) { return a.l<b.l; } bool cmp1(node a,node b) { return a.m<b.m; } void dfs(int x,string s) { if(x==a[num].l) { a[num].str=s; num++; if(num==n) f=1; return ; } dfs(x+1,s+'0'); if(num==n) return; dfs(x+1,s+'1'); }
//字符串从尾部开始改变,0->1,保证相近的字符串在较小的一个的尾部不同,从而保证前者不会成为成为后者的子序列。 int main() { cin>>n; for(int i=0;i<n;i++) { cin>>a[i].l; a[i].m=i; a[i].str=""; } sort(a,a+n,cmp);//按照字符串长度要求排序 dfs(0,""); if(!f) cout<<"NO"<<endl; else { cout<<"YES"<<endl; sort(a,a+n,cmp1);//回复成原来的顺序 for(int i=0;i<n;i++) cout<<a[i].str<<endl; } }
原题链接:http://codeforces.com/problemset/problem/37/C