Codeforces Round #279 (Div. 2) E. Restoring Increasing Sequence

E. Restoring Increasing Sequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.

Restore the the original sequence knowing digits remaining on the board.

Input

The first line of the input contains integer n (1 ≤ n ≤ 105) — the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.

Output

If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers — a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them.

If there is no answer, print a single line "NO" (without the quotes).

Sample test(s)
input
3
?
18
1?
output
YES
1
18
19
input
2
??
?
output
NO
input
5
12224
12??5
12226
?0000
?00000
output
YES
12224
12225
12226
20000
100000

题意:给你n个字符串代表n个数,每个字符串中可能包含?,?可以代替为任何数字,让你求如何使n个数字为严格递增数列。

做法:首先第一个先进行操作,遇到?判断是否是第一位如果是第一位则赋值为1,其他则赋值为0。然后2-n之间进行操作,用l1记录前一个长度,l2记录当前长度,如果l1>l2则直接输出NO结束,如果l1<l2则像第一个字符那样操作,主要麻烦的是l1==l2的情况,首先从首部搜过去,type先记为3,如果搜到当前数字比上一个字符的数字小,标记为type1,并记录当前位置。type1中从记录位置从前搜,搜到当前数字为?并且前一字符的数字不为9。如果当前数字比上一个字符的数字大,则标记为type2,type2中直接进行复制,记录位置前?赋值为前一字符串数字,记录位置后则赋值为0.如果两种情况都不是则是type3,type3从末尾往前搜搜到当前数字为?并且前一字符的数字不为9并记录当前位置。然后进行赋值就行。

模拟过程有点繁琐。

#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <algorithm>
#include<ctime>
#define esp 1e-6
#define LL long long
#define inf 0x0f0f0f0f
using namespace std;
char s[100005][10];
int main()
{
    int n;
    int i,j,k;
    int flag;
    while(scanf("%d",&n)!=EOF)
    {
        flag=1;
        for(i=1;i<=n;i++)
            scanf("%s",s[i]);
        for(j=0;j<strlen(s[1]);j++)
        {
            if(s[1][j]=='?'&&j==0)
                s[1][j]='1';
            if(s[1][j]=='?'&&j!=0)
                s[1][j]='0';
        }
        for(i=2;i<=n;i++)
        {
            int l1,l2,pos,flag1;
            l1=strlen(s[i-1]);
            l2=strlen(s[i]);
            flag1=3;
            if(l1==l2)
            {
                for(j=0;j<l1;j++)
                {
                    if(s[i][j]!='?')
                    {
                        if(s[i][j]>s[i-1][j])
                        {
                            flag1=2;
                            pos=j;
                            break;
                        }
                        if(s[i][j]<s[i-1][j])
                        {
                        flag1=1;
                        pos=j;
                        break;
                        }
                    }
                }
                if(flag1==2)
                {
                    for(j=0;j<pos;j++)
                        if(s[i][j]=='?')
                        s[i][j]=s[i-1][j];
                    for(j=pos+1;j<l1;j++)
                        if(s[i][j]=='?')
                        s[i][j]='0';
                }
                if(flag1==1)
                {
                    int a=0;
                    for(j=pos-1;j>=0;j--)
                    {
                        if(s[i][j]=='?'&&s[i-1][j]!='9')
                        {
                            s[i][j]=s[i-1][j]+1;
                            a=1;
                            break;
                        }
                    }
                    if(a==0)
                    {
                        flag=0;
                        goto loop;
                    }
                    else
                    {
                        for(k=0;k<j;k++)
                            if(s[i][k]=='?')
                            s[i][k]=s[i-1][k];
                        for(k=j+1;k<l1;k++)
                            if(s[i][k]=='?')
                            s[i][k]='0';
                    }
                }
                if(flag1==3)
                {
                    int b=0;
                    for(j=l1-1;j>=0;j--)
                        if(s[i][j]=='?'&&s[i-1][j]!='9')
                    {
                        s[i][j]=s[i-1][j]+1;
                        b=1;
                        break;
                    }
                    if(b==0)
                    {
                        flag=0;
                        goto loop;
                    }
                    else
                    {
                        for(k=0;k<j;k++)
                            if(s[i][k]=='?')
                            s[i][k]=s[i-1][k];
                        for(k=j+1;k<l1;k++)
                            if(s[i][k]=='?')
                            s[i][k]='0';
                    }
                }
            }
            if(l1>l2)
            {
                flag=0;
                goto loop;
            }
            if(l2>l1)
            {
                for(j=0;j<l2;j++)
                {
                    if(s[i][j]=='?'&&j==0)
                        s[i][j]='1';
                    if(s[i][j]=='?'&&j!=0)
                        s[i][j]='0';
                }
            }
        }
        loop:
            if(flag==0)
                printf("NO\n");
            else
            {
                printf("YES\n");
                for(i=1;i<=n;i++)
                    printf("%s\n",s[i]);
            }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值