Codeforces Round #724 (Div. 2)

A. Omkar and Bad Story

题意:

        给你一段非重复数组a(ai <= 100 && ai >= -100)数组,要求你构造长度不超过300的b数组,b数组中必须包含a数组的所有元素,且对于任意的 bi 与 bj 都满足 | bi - bj | 都能在 b 数组内找到。

思路:

        如果一开始的想法是暴力搜索每对 | ai - aj | 来构造的话,如果不含这个元素我们就添加进数组;显然,这个想法实现会很困难,因为新加入的元素可能会与原来的元素构造出新的不存在元素,但是我们可以从中得出,如果存在 ai < 0 的话,那么根本构造不出此数组,所以这个可以作为一个特判。

现在我们知道凭空构造是行不通的,所以继续观察数据,会发现 ai 的范围很小,且去掉小于0的范围,最大只能取到一百,而对于 0 ~ 100 这个数组,必会包含 a 中元素,所以对于每组解,0 ~ 100 都必符合要求,每组解都可以输出 0 ~ 100.

代码实现:

#include<iostream>
using namespace std;

void solve()
{
    int n;
    int f = 0;
    cin >> n;
    for(int i = 0; i < n; i ++)
    {
        int a;
        cin >> a;
        if(a < 0)
            f = 1;
    }
    if(f)
    {
        puts("NO");
        return;
    }
    
    puts("YES");
    cout << 101 << endl;
    for(int i = 0; i <= 100; i ++) cout << i << " ";
    cout << endl;
}

int main()
{
    int t;
    cin >> t;
    while(t --)
    {
        solve();
    }
    return 0;
}

B. Prinzessin der Verurteilung

题意:

        寻找字符串s中不存在的最小字典数串,  s 的长度不超过 1000。

思路:

        我们可以计算 长度为 1 的串有 26 种,长度为 2 的串有 26 ^ 2 = 676 种, 长度为 3 的字符串有26^3 =  17576 种,那么在可以知道,在长度不超过1000的串中,不存在的最小字串长度必不会超过3,所以我们可以长度不超过3的 s 的子串放入集合,然后暴力枚举所有长度不超过 3 的字串寻找答案。(其实如果把长度为 n 的字串全部放入集合也可以,因为我们查找的时候子串长度肯定不会超过 3)。

代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<unordered_map>
#include<queue>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;


void solve()
{
    unordered_map<string, int>mp;
    int n;
    cin >> n;
    string s;
    cin >> s;
    for(int l = 0; l < 3; l ++)
    {
        for(int i = 0; i < n - l; i ++)
        {
            string t = "";
            int j = i + l;
            for(int k = i; k <= j; k ++)
            {
                t += s[k];
            }
            mp[t] ++;
        }
    }

    queue<string>Q;
    for(int i = 0; i < 26; i ++)
    {
        char x = 'a' + i;
        string t = "";
        t.push_back(x);
        Q.push(t);
    }

    while(Q.size())
    {
        string cur = Q.front();
        Q.pop();
        if(!mp[cur])
        {
            cout << cur << endl;
            break;
        }
        for(int i = 0; i < 26; i ++)
        {
            char p = 'a' + i;
            string t = cur;
            t.push_back(p);
            Q.push(t);
        }
    }
}

int main()
{
    int t;
    cin >> t;
    while(t --) solve();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值