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


  • 相关阅读:
    git 学习网站
    Vue 部署在 IIS 上
    Element UI 的坑
    Vue 中 Prop 传至的 一个Bug
    Asp.net Core 部署在 IIS上
    今天用UniApp开发, 用到 Vuex 中的 mutations, 设置值的时候好像只能传2个参数, 第一个是固定的state, 第二个是一个值, 不能传第三个了
    anxios 和 uni.request 访问Asp.net 服务器传参出错的坑
    内网计算机设置问题说明
    关于综合布线
    Android学习 -- Activity 以及Activity之间值传递
  • 原文地址:https://www.cnblogs.com/BIGTOM/p/8447265.html
Copyright © 2011-2022 走看看