1 solutions

  • 0
    @ 2024-6-18 13:36:00

    模拟(55pts)

    • 55%的测试数据说明了地址串,符合规范,那么我们根据按照字符串来模拟题目过程即可得到55分
    #include<bits/stdc++.h>
    using namespace std;
    map<string,int> server;
    int main()
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            string s,t;
            cin>>s>>t;
            if(s=="Server") //服务器
            {
                if(server[t]==0) //还没有出现过
                {
                    cout<<"OK"<<endl;
                    server[t]=i; //建立连接
                }
                else //建立失败
                {
                    cout<<"FAIL"<<endl;
                }
            }
            else
            {
                 if(server[t]==0) //没有对应的服务器
                {
                    cout<<"FAIL"<<endl;
                }
                else
                {
                    cout<<server[t]<<endl; //输出对应的服务器编号
                }
            }
        }
        return 0;
    }
    

    模拟(100pts)

    • 在上一个的基础上加上地址合法判断即可。
    #include<bits/stdc++.h>
    using namespace std;
    map<string,int> server;
    bool check(string s)
    {
        int a[10]={0,-1,-1,-1,-1,-1};
        int cnt=sscanf(s.c_str(),"%d.%d.%d.%d:%d",&a[1],&a[2],&a[3],&a[4],&a[5]); //按照格式化读入读到的数字个数
        if(cnt<5) return false; //数字个数不够五个
        for(int i=1;i<=4;i++) //判断ip地址是否合法
        {
            if(a[i]<0||a[i]>255) return false;
        }
        if(a[5]<0||a[5]>65535) return false; //判断端口是否合法
        char ss[110];
        sprintf(ss,"%d.%d.%d.%d:%d",a[1],a[2],a[3],a[4],a[5]); //还原回去
        if(string(ss)!=s) return false; //不相同
        return true;
    }
    int main()
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            string s,t;
            cin>>s>>t;
            if(!check(t)) //当前地址或端口不合法
            {
                cout<<"ERR"<<endl;
                continue;
            }
            if(s=="Server") //服务器
            {
                if(server[t]==0) //之前是空服务器
                {
                    cout<<"OK"<<endl;
                    server[t]=i;
                }
                else //之前已经有服务器了
                {
                    cout<<"FAIL"<<endl;
                }
            }
            else
            {
                if(server[t]==0) //服务器为空
                {
                    cout<<"FAIL"<<endl;
                }
                else //可以找到对应的服务器
                {
                    cout<<server[t]<<endl;
                }
            }
        }
        return 0;
    }
    
    • 1

    Information

    ID
    1073
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    10
    Tags
    # Submissions
    2
    Accepted
    2
    Uploaded By