Files
python/TangDou/CSP-J/J2-2021/3.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

63 lines
1.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
int n;
// sscanf和sprintf详解
// https://blog.csdn.net/qq_40563761/article/details/107087811
/*
sscanf与sprintf
sscanf(str,"%d",&n) 其实就是把str的内容以"%d"的格式写入到n中(从左到右)
同理 sprintf(str,"%d",n)就是把n以"%d"的格式写入到str (从右到左)
*/
bool check(string s) {
int a, b, c, d, port;
//字符串格式化提取+判断转换成功个数
if (sscanf(s.c_str(), "%d.%d.%d.%d:%d", &a, &b, &c, &d, &port) != 5) return false;
//需符合IP地址规则
if (a < 0 || a > 255 || b < 0 || b > 255 || c < 0 || c > 255 || d < 0 || d > 255 || port < 0 || port > 65535)
return false;
/*
居然有这个恶心的用例:
179.90.115.156:54168:
解决的办法:把分离出的数字再组装回去,和原来一样就是OK,否则就不OK!
*/
// if ((string)ss != s) {
// cout << "ss=" << (string)ss << endl;
// cout << s << endl;
// }
char ss[25]; // 3*4+3+5=20
sprintf(ss, "%d.%d.%d.%d:%d", a, b, c, d, port);
return (string)ss == s;
}
map<string, int> _map;
string op, ip;
int main() {
//文件输入
freopen("P7911_13.in", "r", stdin);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> op >> ip;
if (!check(ip)) {
cout << "ERR" << endl;
continue;
}
if (op == "Server") {
if (_map[ip])
puts("FAIL");
else
_map[ip] = i, puts("OK");
} else {
if (_map[ip] == 0)
puts("FAIL");
else
printf("%d\n", _map[ip]);
}
}
return 0;
}