判断账户名是否 okay

Tag: 笔试算法题 Posted on 2022-03-25 10:58:50 Edited on 2022-03-25 10:58:50 Views: 145

概述

给你一堆用户名,判断是否符合要求:

  1. 长度限制,
  2. 字符限制,
  3. 之前没注册过

解法

别问里面为啥有两个 typo,问就是题目里面这两个单词就拼错了,笑死。

我不理解为啥只过了 89%。

#include <bits/stdc++.h>
using namespace std;

int main() {
    int T; cin >> T;
    unordered_set<char> validChars;
    unordered_set<string> accounts;
    for (char c = 'a'; c <= 'z'; c ++) {
        validChars.insert(c);
    }
    for (char c = 'A'; c <= 'Z'; c ++) {
        validChars.insert(c);
    }
    for (int t = 0; t < T; t ++) {
        string s; cin >> s;
        if (s.size() < 6 || s.size() > 12) {
            cout << "illegal length" << endl;
            continue;
        }
        bool ok = true;
        for (auto c : s) {
            if (validChars.count(c) == 0) {
                cout << "illegal charactor" << endl;
                ok = false;
                break;
            }
        }
        if (!ok) continue;
        if (accounts.count(s) != 0) {
            cout << "acount existed" << endl;
        } else {
            accounts.insert(s);
            cout << "registration complete" << endl;
        }
    }
}

未经允许,禁止转载,本文源站链接:https://iamazing.cn/