CF 37 C Old Berland Language【模拟+dfs】

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.

Example
Input
3
1 2 3
Output
YES
0
10
110
Input
3
1 1 1
Output
NO

题意:题目中给出N个长度,构造01串来表示每一个古汉语,注意,任何一个字符串不能是别的字符串的前缀。

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <cmath>
using namespace std;
string ans[1200][1200];
int flag[1200];
struct node
{
    int pos;
    int i;
    friend bool operator < (node a,node b)
    {
        return a.pos<b.pos;
    }
}a[1200];

int pos=0;

int n;

bool cmd(node a,node b)
{
    return a.i<b.i;
}

void dfs(int len ,string now)
{
    if(pos==n) return;
    for(int i=len;i<=1001;i++){
        if(i==1001) return;
        if(flag[i]) break;
    }
    if(flag[len]){
        ans[len][flag[len]--]=now+"1";
        pos++;
        if(flag[len]){
            ans[len][flag[len]--]=now+"0";
            pos++;
        }else{
            dfs(len+1,now+"0");
        }
    }else{
        dfs(len+1,now+"0");
        dfs(len+1,now+"1");
    }
}


int main()
{
    scanf("%d",&n);
    int cnt[1200];
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i].pos);
        a[i].i=i;
        flag[a[i].pos]++;
        cnt[a[i].pos]=flag[a[i].pos];
    }
    string s="";
    dfs(1,s);
    if(pos!=n){
        printf("NO\n");
    }else{
        printf("YES\n");
        for(int i=1;i<=n;i++){

            cout<<ans[a[i].pos][cnt[a[i].pos]--]<<endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值