题解:#2093.「CSP-J 2021」网络连接(network) 审核通过

lyhmbr 我要拿一等奖 2024-07-19 15:27:27 11

题目大意

给你两个操作类型 以下简称

然后给你一串形如 的字符串 以下称为

当操作类型为

  1. 合法且是先前没有与其冲突的 输出
  2. 非法,输出
  3. 合法但是有冲突项 输出

当操作类型为

  1. 合法且没有冲突 输出 对应的编号
  2. 非法,输出
  3. 合法但是有冲突项 输出

分析

题目有两大难点

  1. 关于每个 的编号与查找
  2. 分析 是否合法

解决

刚好,有两个神器解决了问题 绝对不是因为懒

  • 中的一个容器,其实现是红黑树,关于 的使用:map<key,val> 意思大概是 可以很方便的解决 的编号和查找问题

  • 不多赘述

  • 的输入输出流

代码

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <map>
#include <sstream>
using namespace std;

//本题用到map和sscanf;

map<string, int> maps;

bool is_ok(string str) {
    int a, b, c, d, e;
    sscanf(str.c_str(), "%d .%d .%d .%d :%d", &a, &b, &c, &d, &e);
    if (a < 0 || a > 255 || b < 0 || b > 255 || c < 0 || c > 255 || d < 0 || d > 255 || e < 0 || e > 65535) {
        return false;
    }
    //检查是否前导零
    stringstream strstr;
    strstr << a << "." << b << "." << c << "." << d << ":" << e;

    return strstr.str() == str;
}

int main() {
    freopen("network.in", "r", stdin);
    freopen("network.out", "w", stdout);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        string a, b;
        cin >> a;
        cin >> b;
        if (!is_ok(b)) {
            cout << "ERR" << endl;
        } else {
            if (a[0] == 'S') {
                if (maps[b]) {
                    cout << "FAIL" << endl;
                } else {
                    maps[b] = i;
                    cout << "OK" << endl;
                }
            } else {
                if (!maps.count(b)) {
                    cout << "FAIL" << endl;
                } else {
                    cout << maps[b] << endl;
                }
            }
        }
        // cout<<maps[b]<<" "<<b<<endl;
    }
}
{{ vote && vote.total.up }}