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

    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??
    ????
    */


  • 相关阅读:
    成立移动互联网公司???
    C++的子对象
    单链表 操作的18种算法
    再论虚函数
    多线程(三)
    多线程(二)
    多线程(一)
    存储过程的参数
    git
    多态(三)
  • 原文地址:https://www.cnblogs.com/BIGTOM/p/8447265.html
Copyright © 2011-2022 走看看