B. Prinzessin der Verurteilung(求从小到大第一个没出现过的字符串)

题目:http://codeforces.com/contest/1536/problem/B

题意:给你一串字符串,然后按字典序求第一个没在这个字符串中出现的子串,如样例:qaabzwsxedcrfvtgbyhnujmiklop,a-z都出现了,aa出现了,ab出现了,ac没出现,答案就是ac。

题解:由于数据范围不大只有1000,先把题目给的那个字符一段一段截取下来存到set里面,大概长度为4就够题目给的范围了,,然后暴力从a往后开始扫看哪个不再set里面。

代码:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
//#include <unordered_set>
//#include <bits/stdc++.h>
//#define int long long
#define pb push_back
#define PII pair<int, int>
#define mpr make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define x first
#define y second
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e6 + 7;
using namespace std;

set<string> se, se2;
int main() {
    ios::sync_with_stdio(0);
    int T;
    cin >> T;
    while (T--) {
        int n;
        string s;
        cin >> n >> s;
        se.clear();
        for (int j = 0; j <= 3; j++) { //预处理
            for (int i = 0; i < n; i++) {
                string str;
                for (int k = i; k < min(n, i + j); k++) {
                    str += s[k];
                }
                se.insert(str);
            }
        }
        string str;
        char ch = 'a'; //字符串中最后一个数
        while (1) {  //暴力找
            if (!se.count(str + ch)) {//不在set里就找到了
                cout << str + ch << endl;
                break;
            }
            if (ch == 'z') {//如果最后一个数为z,那么就让前面进位
                ch = 'a';
                int len = str.size();
                while (len != 0 && str[len - 1] == 'z') { //看看是否还有z,有的话就把这一位变为1
                    string q = str.substr(0, len - 1);
                    q = q + 'a' + str.substr(len);
                    str = q;
                    len--;//往前找
                }
                if (len == 0) { //如果全部为z,那么上面就全部改为a了,然后再加个长度
                    str += 'a';
                    // cout << str + ch << endl;
                    continue;
                }
                if (str[len - 1] != 'z') {//如果中间有个不为z,那么就让这一位++
                    string q = str.substr(0, len - 1);
                    q = q + ((char)(str[len - 1] + 1)) + str.substr(len);
                    str = q;
                }
            } else {//否则最后一位++
                ch++;
            }
            // cout << str + ch << endl;
            // getchar();
        }
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值