网络连接

jiaoyuyuan 2024-05-04 14:04:38 3 返回题目

#include<bits/stdc++.h>

using namespace std;

map<string,int> x;

int n;

bool check(char s[]){

int a=-1,b=-1,c=-1,d=-1,e=-1;
int t=sscanf(s,"%d.%d.%d.%d:%d",&a,&b,&c,&d,&e);
if(t!=5) return 0;
if(a<0||a>=255) return 0;
if(b<0||b>=255) return 0;
if(c<0||c>=255) return 0;
if(d<0||d>=255) return 0;
if(e<0||e>=65535) return 0;
char s2[35];
sprintf(s2,"%d.%d.%d.%d:%d",a,b,c,d,e);
int l=strlen(s);
bool f=0;
for(int i=0;i<l;i++){
    if(s[i]==s2[i])f=1;
    else{
        f=0;
        break;
    }
}
return f;

} int main(){

freopen("network.in","r",stdin);
freopen("network.out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++){
    char a[1005],b[1005];
    cin>>a>>b;
    string t(b);
    if(a[0]=='S'){
        if(!check(b)) cout<<"ERR"<<endl;
        else if(x.count(t)!=0){
            cout<<"FAIL"<<endl;
        } 
        else{
            cout<<"OK"<<endl;
            x[t]=i;
        }
    }
    else{
        if(!check(b)){
            cout<<"ERR"<<endl; 
        }
        else if(x.count(t)==0){
            cout<<"FAIL"<<endl;
        }
        else{
            cout<<x[b]<<endl;
        }
    }
}
return 0;

}

{{ vote && vote.total.up }}

共 1 条回复

cookiebus
#include <bits/stdc++.h>
#define int long long

using namespace std;
map<string, int> server;
int cnt(string s, char c) {
    int r = 0;
    for (int i = 0; i < s.size(); ++i)
        if (s[i] == c)
            r++;
    return r;
}
bool chk(int p, int l, int r) { return p >= l && p <= r; }
bool Judge(string s) {
    if (cnt(s, '.') != 3)
        return false;
    if (cnt(s, ':') != 1)
        return false;
    int num = -1;
    for (int i = 0; i < s.size(); ++i) {
        if (s[i] == '.' || s[i] == ':') {
            if (!chk(num, 0, 255))
                return false;
            num = -1;
        } else {
            if (num == 0)
                return false;
            if (num == -1)
                num = s[i] - '0';
            else
                num = num * 10 + s[i] - '0';
        }
    }
    if (!chk(num, 0, 65535))
        return false;
    return true;
}

signed main() {
    freopen("network.in", "r", stdin);
    freopen("network.out", "w", stdout);
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        string op, ip;
        cin >> op >> ip;
        if (!Judge(ip))
            cout << "ERR" << endl;
        else if (op == "Client") {
            if (server[ip] == 0)
                cout << "FAIL\n";
            else
                cout << server[ip] << endl;
        } else {
            if (server[ip] != 0)
                cout << "FAIL\n";
            else {
                cout << "OK" << endl;
                server[ip] = i;
            }
        }
    }
    return 0;
}```