Codeforces_490_E

http://codeforces.com/problemset/problem/490/E

 

dfs,过程要注意细节,特别是当前位置取了与上个数当前位置相同是,若后面不符合条件,则当前位置要重置'?'。

 

#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
string a[100005];

bool dfs(int i,int j, bool flag)
{
    
    if(j == a[i].size())    return flag;
    if(flag)
    {
        if(a[i][j] == '?')  a[i][j] = '0';
        return dfs(i,j+1,flag);
    }
    if(a[i][j] == '?')
    {
        a[i][j] = a[i-1][j];
        if(dfs(i,j+1,flag))    return 1;
        else if(a[i][j] == '9')
        {
            a[i][j] = '?';
            return 0;
        }
        else
        {
            a[i][j]++;
            return dfs(i,j+1,1);
        }
    }
    if(a[i][j] > a[i-1][j])    return dfs(i,j+1,1);
    if(a[i][j] == a[i-1][j])    return dfs(i,j+1,flag);
    else return 0;
}
int main()
{
    a[0] = "0";
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++)    cin >> a[i];
    for(int i = 1;i <= n;i++)
    {
        if(a[i].size() > a[i-1].size())
        {
            for(int k = 0;k < a[i].size();k++)
            {
                if(a[i][k] == '?')
                {
                    if(k == 0)    a[i][k] = '1';
                    else         a[i][k] = '0'; 
                }
            }
        }
        else if(a[i].size() < a[i-1].size())
        {
            cout << "NO" << endl; 
            return 0;
        }
        else if(!dfs(i,0,0))
        {
            cout << "NO" <<endl;
            return 0;
        }
    }
    cout << "YES" << endl;
    for(int i = 1;i <= n;i++)    cout << a[i] << endl;
    return 0; 
}

 

posted @ 2016-09-14 15:22  zzzzzzzzhu  阅读(160)  评论(0编辑  收藏  举报