zoukankan      html  css  js  c++  java
  • 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

     思路: 1-n 处理,前面的已经确定了,处理的i 大于 i-1,而且取的尽量小

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #define maxn 100010
    using namespace std ;
    
    char ans[maxn][10] ;
    bool solve(char aa[],char b[])
    {
        int n,m,i,j;
        char a[10] ;
        strcpy(a,aa) ;
        n = strlen(a) ;
        m = strlen(b) ;
        //cout<<n<<" "<<m<<endl;
        if(n<m) return false ;
        if(n>m)
        {
            if(a[0]=='?')a[0]='1';
            for( i = 1;i < n ;i++)if(a[i]=='?')
                a[i]='0';
            strcpy(aa,a) ;
            return true;
        }
        bool flag=false ;
        a[n]='1';
        b[n]='2';
        for(i = 0 ; i <= n ;i++)
        {
            if(a[i]=='?')
            {
                if(flag)a[i]='0';
                else a[i]=b[i] ;
            }
            else if(a[i]>b[i])flag=true;
            else if(a[i]<b[i])
            {
                if(flag) continue ;
                for( j = i ; j < n ;j++)
                    if(a[j]=='?') a[j]='0' ;
                for(j = i ; j >= 0 ;j--)
                {
                    if(aa[j]=='?')
                    {
                        if(b[j]=='9')a[j]='0' ;
                        else
                        {
                            a[j]=b[j]+1;
                            a[n]='' ;
                            strcpy(aa,a) ;
                            return true;
                        }
                    }
                }
                return false;
            }
        }
        a[n]='' ;
        strcpy(aa,a) ;
        return true;
    }
    int main()
    {
        int i,n,m,j,k;
        char pre[10] ;
        while(scanf("%d",&n) != EOF)
        {
            for(i = 1 ; i <= n ;i++)
                scanf("%s",ans[i]) ;
            pre[0]='0';
            pre[1]='';
            for(i = 1 ; i <= n ;i++)
            {
                if(!solve(ans[i],pre)) break ;
                strcpy(pre,ans[i]);
            }
            if(i!=n+1)puts("NO") ;
            else
            {
                puts("YES") ;
                for( i = 1 ; i <= n ;i++)
                   printf("%s
    ",ans[i]) ;
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    C++学习笔记之 C++对C的增强和拓展
    C++学习笔记之 命名空间
    Python学习笔记之 MySQL
    C++学习笔记之 命名空间
    C++学习笔记之 开始
    C++学习笔记之 环境搭建
    C++学习笔记之 初识与概述
    数据结构与算法笔记(一) 数据结构与算法绪论
    C语言项目案例之 贪吃蛇
    C语言学习笔记之 文件操作
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/4134723.html
Copyright © 2011-2022 走看看