5分求调(wa)
查看原帖
5分求调(wa)
581411
sunyuyang楼主2022/7/30 18:32
#include <bits/stdc++.h>
using namespace std;

int n, k;
struct po
{
    string name;
    int n;
};

vector<po> Server; //储存已建立连接的服务器名称及编号

void input()
{
    cin >> n;
    k = n;
}
//判断地址是否合法
bool check(string res)
{
    vector<int> v;
    int k = 0, h = 0, len = res.size();
    for (int i = 0; i < len; i++)
    {
        if (res[i] >= '0' && res[i] <= '9')
        {
            v.push_back(res[i] - '0'); //字符数字转整形数字
            h++;
        }
        if (res[i] == '.' || res[i] == ':' || i == len - 1)
        {
            int x = 0;
            k++;
            if (res[i] == ':' && k <= 3) //前三串数字后不能是“:”
            {
                return 0;
            }
            if (res[i] == '.' && k == 4) //第四串数字后不能是“.”
            {
                return 0;
            }
            for (int i = 0; i < h; i++)
            {
                if (i == 0 && v[i] == 0 && h >= 2) //若为一位数,0仍合法
                {
                    return 0;
                }
                x = x + v[i] * pow(10, h - i - 1);
                v.clear();
            }
            if (k <= 4)
            {
                if (x < 0 || x > 255)
                    return 0;
            }
            if (k == 5)
            {
                if (x < 0 || x > 65535)
                    return 0;
            }
            h = 0;
        }
    }
    return 1;
}
//检查能否连接
int checkl(string s)
{
    for (int i = 0; i < Server.size(); i++)
    {
        if (Server[i].name == s)
            return Server[i].n;
    }
    return 0;
}
//查重
bool repeat(string s)
{
    for (int i = 0; i < Server.size(); i++)
    {
        if (Server[i].name == s)
            return 0;
    }
    return 1;
}

void work()
{
    while (n--)
    {
        string s, adres;
        cin >> s >> adres;
        if (check(adres) == 0) //如果不符合规范
        {
            cout << "ERR" << endl;
            continue;
        }
        else
        {
            if (s == "Server")
            {
                int ss = repeat(adres);
                if (ss == 1)
                {
                    cout << "OK" << endl;
                    Server.push_back({adres, k - n}); //将地址和编号放入队列,建立连接
                }
                else
                {
                    cout << "FALL" << endl;
                    continue;
                }
            }
            else
            {
                int ans = checkl(adres);
                if (ans == 0)
                { //若无法连接
                    cout << "FALL" << endl;
                    continue;
                }
                else
                {
                    cout << ans << endl;
                    continue;
                }
            }
        }
    }
}

int main()
{
    input();
    work();
    return 0;
}
2022/7/30 18:32
加载中...