牛客小白月赛4 B 博弈论 思维 字符串

链接:https://www.nowcoder.com/acm/contest/134/B
来源:牛客网

题目描述

铁子和顺溜在学习了博弈论的sg函数之后,解决了很多很多博弈题,现在他们遇到了一道难题。
给出一个长度为 n 的数列,数列里的每个元素都是个位数,这个数列的每一个连续子数列都能生成
一个十进制数,对于子数列a[l~r],这个十进制数的个位为a[r],十位为a[r - 1],...,最高位
为a[l]。
现在铁子需要知道最小的不能被该数列的子数列生成的十进制非负整数是多少?

输入描述:

第一行一个数字n。(1 ≤ n ≤ 1000)
第二行n个数字d
i
。(0 ≤ d
i
 ≤ 9)

输出描述:

输出一个数字表示答案。
示例1

输入

复制
4
3 0 1 2

输出

复制
4
示例2

输入

复制
10
9 8 7 6 5 4 3 2 1 0

输出

复制
11

分析:直接枚举所有可能组成的数放入map(注意数会很大用字符串存下来,同时考虑前导0的情况),然后从0开始往后找,第一个map里没有的数字就是所不能组成的最小的数
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e3+10;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
string f( string t ) {
    string s = "";
    for( ll i = t.length()-1; i >= 0; i -- ) {
        s += t[i];
    }
    return s;
}
int main() {
    ios::sync_with_stdio(0);
    ll n, a[maxn];
    map<string,ll> mp;
    cin >> n;
    for( ll i = 0; i < n; i ++ ) {
        cin >> a[i];
    }
    for( ll i = 0; i < n; i ++ ) {
        string t = "";
        char c = a[i] + '0';
        t = t + c;
        mp[t] ++;
        if( t == "0" ) {
            t = "";
        }
        for( ll j = i+1; j < n; j ++ ) {
            c = a[j] + '0';
            if( t.length() == 0 && c == '0' ) { //去掉前导0
                continue;
            }
            t = t + c;
            mp[t] ++;
        }
    }
    for( ll i = 0; i <= maxn*maxn; i ++ ) {
        string t = "";
        ll num = i;
        if( num == 0 ) {
            t = t + '0';
        } else {
            while(num) {
                char c = num%10 + '0';
                t = t + c;
                num /= 10;
            }
        }
        t = f(t);
        if( !mp[t] ) {
            cout << t << endl;
            break;
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/l609929321/p/9531914.html