2021CSP-J 普及组第二轮试题及解析( 第三题网络连接network)

#include <iostream>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
using namespace std;
set<string> server;
map<string,int> mp;
bool judge(int x, int y, int z)
{
    if(x < y || x > z) return false;
    return true;
}

bool check(string str)
{
    stringstream fin(str);
    int a, b, c, d, e;
    char x, y, z, w;
    fin>>a>>x>>b>>y>>c>>z>>d>>w>>e;
    
    if(!judge(a,0,255) || !judge(b,0,255) || !judge(c,0,255) || !judge(d,0,255) || !judge(e,0,65535))
        return false;
    if(x != '.' || y != '.' || z != '.' || w != ':') return false;
    string temp = to_string(a)+"."+to_string(b)+"."+to_string(c)+"."+to_string(d)+":"+to_string(e);
    return temp == str;
}
int main()
{
    int n;
    cin>>n;
    for(int i = 1; i <= n; i++)
    {
        string op, str;
        cin>>op>>str;
        bool ok = check(str);
        if(op[0] == 'S')
        {
            if(!ok) cout<<"ERR"<<endl;
            else
            {
                if(server.find(str) == server.end())
                {
                    cout<<"OK"<<endl;
                    server.insert(str);
                    mp[str] = i;
                }
                else cout<<"FAIL"<<endl;
            }
        }
        else
        {
            if(!ok) cout<<"ERR"<<endl;
            else
            {
                if(server.find(str) != server.end())
                {
                    map<string,int>::iterator it;
                    for(it = mp.begin(); it != mp.end(); it++)
                    {
                        string p = it->first;
                        if(p == str)
                        {
                            cout<<it->second<<endl;
                            break;
                        }
                    }
                }
                else cout<<"FAIL"<<endl;
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值