
简单描述一下我的代码思路:
check 函数是判断是否合法(即 ERR),maohao 是记录冒号出现过没有,cnt_dot 记录的是 . 出现了几次,now 是当前数的值。
两个 map 就无需多言了,看名字就知道是干什么的。
代码:
#include <bits/stdc++.h>
#define _for(i, a, b) for (int i = (a); i <= (b); i ++ )
using namespace std;
int q;
map<string, int> server, client;
bool check(string s) {
bool maohao = false;
int cnt_dot = 0, now = 0;
_for (i, 0, s.size() - 1) {
if (s[i] == ':') {
if (maohao) return false;
maohao = true;
if (now < 0 || now > 255) return false;
now = 0;
}
else if (s[i] == '.') {
cnt_dot ++ ;
if (cnt_dot > 3) return false;
if (maohao) return false;
if (now < 0 || now > 255) return false;
now = 0;
}
else {
if (i && (s[i - 1] == '.' || s[i - 1] == ':') && s[i] == '0')
if (i + 1 < s.size() && s[i + 1] != '.' && s[i + 1] != ':') return false;
now = now * 10 + (s[i] - '0');
}
}
if (now < 0 || now > 65535) return false;
if (cnt_dot != 3) return false;
if (! maohao) return false;
return true;
}
signed main() {
ios :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> q;
string type, x;
_for (i, 1, q) {
cin >> type >> x;
if (! check(x)) { puts("ERR"); continue; }
if (type == "Server") { if (server[x]) puts("FAIL"); else puts("OK"), server[x] = i; }
else { if (! server[x]) puts("FAIL"); else cout << server[x] << endl, client[x] = i; }
}
return (2e9 - 1e9 - 1e9);
}