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).

Examples
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

思路:贪心,顺序遍历每个数,每次找到最小合法值,若找不到,则“NO”。复杂度O(n*2^L),L是字符串长度。
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cctype>
#include <cassert>
#include <bitset>
#include <ctime>

using namespace std;

#define pau system("pause")
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define clr(a, x) memset(a, x, sizeof(a))

const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

int n;
string s[100015];
bool cmp(int x, int i) {
    if ('?' == s[x][i]) {
        if (i < (int)s[x].length() - 1) {
            if (cmp(x, i + 1)) {
                s[x][i] = s[x - 1][i];
                return true;
            }
        }
        if ('9' == s[x - 1][i]) {
            return false;
        } else {
            s[x][i] = s[x - 1][i] + 1;
            for (int j = i + 1; j < s[x].length(); ++j) {
                if ('?' == s[x][j]) {
                    s[x][j] = '0';
                }
            }
            return true;
        }
    }
    if (s[x][i] == s[x - 1][i]) {
        if (i == (int)s[x].length() - 1) {
            return false;
        } else {
            return cmp(x, i + 1);
        }
    } else if (s[x][i] > s[x - 1][i]) {
        for (int j = i + 1; j < s[x].length(); ++j) {
            if ('?' == s[x][j]) {
                s[x][j] = '0';
            }
        }
        return true;
    } else {
        return false;
    }
}
bool dfs(int x) {
    if (s[x - 1].length() > s[x].length()) {
        return false;
    }
    if (s[x - 1].length() < s[x].length()) {
        if ('?' == s[x][0]) s[x][0] = '1';
        for (int j = 1; j < s[x].length(); ++j) {
             if ('?' == s[x][j]) {
                s[x][j] = '0';
             }
        }
        return true;
    }
    return cmp(x, 0);
}
int main() {
    ios::sync_with_stdio(false);
    cin >> n;
    s[0] = "0";
    for (int i = 1; i <= n; ++i) {
        cin >> s[i];
    }
    for (int i = 1; i <= n; ++i) {
        if (!dfs(i)) {
            cout << "NO" << endl;
            return 0;
        }
    }
    cout << "YES" << endl;
    for (int i = 1; i <= n; ++i) {
        cout << s[i] << endl;
    }
    return 0;
}
/*
6
??4?
??3?
????
121?
?0??
????
*/

 



转载于:https://www.cnblogs.com/BIGTOM/p/8447265.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值