记录编号 574605 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [CSP 2021J]网络连接 最终得分 100
用户昵称 Gravatar惠惠 是否通过 通过
代码语言 C++ 运行时间 0.136 s
提交时间 2022-08-08 15:55:06 内存使用 0.00 MiB
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;

int n;
bool sc[1010] = {1};
string ip[1010] = {"None"};

bool check(string str)
{
    int point = 0, mao = 0;
    for(int i = 0; str[i] != '\0'; ++i)
    {
        if(str[i] == '.') ++point;
        if(str[i] == ':') ++mao;
    }
    if(point != 3 || mao != 1)
    {
        // cout << "符号数量错误" << endl;
        return false;
    }
    int point_p = 0;
    for(int i = 0; str[i] != '\0'; ++i)
    {
        if(str[i] == '.')
        {
            if(i - point_p > 3) 
            {
                // cout << "点间数字位数错误" << endl;
                return false;
            }
            point_p = i + 1;
        }
        if(str[i] == ':' && i - point_p > 3)\
        {
            // cout << "冒号前数字位数错误" << endl; 
        }
        
    }
    point_p = 0;
    for(int i = 0; str[i] != '\0'; ++i)
    {
        if(str[i] == '.')
        {
            if(i == point_p) return false;
            if(str[point_p] == '0' && i - point_p != 1) return false;
            int num = 0;
            for(int j = point_p; j <= i - 1; ++j)
            {
                num = num * 10 + (str[j] - '0');
                if(num > 255 || num < 0)
                {
                    // cout << "IP超界" << endl;
                    return false;
                }
            }
            point_p = i + 1;
        }
        if(str[i] == ':')
        {
            if(i == point_p) return false;
            if(str[i + 1] == '\0') return false;
            if(str[point_p] == '0' && i - point_p != 1) return false;
            if(str[i + 1] == '0' && str[i + 2] != '\0') return false;
            int num = 0;
            for(int j = point_p; j <= i - 1; ++j)
            {
                num = num * 10 + (str[j] - '0');
                if(num > 255 || num < 0)
                {
                    // cout << "IP超界" << endl;
                    return false;
                }
            }
            num = 0;
            for(int j = i + 1; str[j] != '\0'; ++j)
            {
                num = num * 10 + (str[j] - '0');
                // cout << "num: " << num << endl;
                if(num > 65535 || num < 0)
                {
                    // cout << "端口超界" << endl;
                    return false;
                }
            }
            return true;
        }
    }
    return true;
}

int main()
{
    freopen("csp2021pj_network.in", "r", stdin);
    freopen("csp2021pj_network.out", "w", stdout);
    cin >> n;
    for(int i = 1; i <= n; ++i)
    {
        string S_C;
        cin >> S_C;
        if(S_C == "Server")
        {
            sc[i] = 0;
            string str;
            cin >> str;
            if(!check(str))
            {
                cout << "ERR" << endl;
                continue;
            }
            bool fall = false;
            for(int j = 1; j <= n; ++j)
            {
                if(ip[j] == str && sc[j] == 0)
                {
                    cout << "FAIL" << endl;
                    fall = true;
                    break;
                }
            }
            if(!fall)
            {
                cout << "OK" << endl;
                ip[i] = str;
            }
        }
        else
        {
            string str;
            cin >> str;
            if(!check(str))
            {
                cout << "ERR" << endl;
                continue;
            }
            bool fall = true;
            for(int j = 1; j <= n; ++j)
            {
                if(sc[j] == 0 && ip[j] == str)
                {
                    cout << j << endl;
                    fall = false;
                }
            }
            if(fall) cout << "FAIL" << endl;
        }
    }
    return 0;
}