CSP-J 2021 T3 网络连接

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int idx;
unordered_map <string, int> mp;
unordered_map <string, int> mp2;
bool check(string ad) {
    int dsum = 0;
    int msum = 0;

    for (int i = 0; i < ad.size(); i++) {
        if (dsum != 3 && msum == 1) {
            return false;
        }

        if (ad[i] == '.') {
            dsum++;
        } else if (ad[i] == ':') {
            msum++;
        } else if (ad[i] >= '0' && ad[i] <= '9') {
            continue;
        } else {
            return false;
        }
    }

    if (dsum

CSP - J 2021网络连接题目代码实现有不同的方式,以下给出两种代码示例。 ### 55分代码 此代码可以拿到测试点1到11的分,核心思路是根据输入的操作类型(服务器或客户端)和地址串,判断是否满足相应条件并输出结果。 ```cpp #include <bits/stdc++.h> using namespace std; //看数据范围:根据特殊性质1:保证所有的地址串均符合规范 //拿测试点1到11的分:55分 int n; string op[1010],ad[1010]; bool f=1; int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>op[i]>>ad[i]; f=1; if(op[i]=="Server"){ for(int j=1;j<i;j++){ if(op[j]=="Server" && ad[j]==ad[i]){ cout<<"FAIL"<<endl; f=0; break; } } if(f==1) cout<<"OK"<<endl; } f=1; if(op[i]=="Client"){ for(int j=1;j<i;j++){ if(op[j]=="Server" && ad[j]==ad[i]){ cout<<j<<endl; f=0; break; } } if(f==1) cout<<"FAIL"<<endl; } } return 0; } ``` ### 100分代码 使用C++11的regex库,通过正则表达式判断地址串是否合法,再根据操作类型进行相应处理。 ```cpp #include <bits/stdc++.h> using namespace std; int n; regex r("(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5]):(\\d|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])"); map<string, int> m; string o, a; int main(int argc, char const *argv[]) { cin >> n; for (int i = 1; i <= n; i++) { cin >> o >> a; if (!regex_match(a, r)) { cout << "ERR\n"; continue; } if (o[0] == &#39;S&#39;) { if (m[a]) cout << "FAIL\n"; else m[a] = i, cout << "OK\n"; } else { if (!m.count(a)) cout << "FAIL\n"; else cout << m[a] << &#39;\n&#39;; } } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值