Files
python/TangDou/CSP-J/AcWingTraining/T1/4088.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

53 lines
1.4 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 w[5]; // 分解数组
bool check(string addr) {
memset(w, -1, sizeof w);
sscanf(addr.c_str(), "%d.%d.%d.%d:%d", &w[0], &w[1], &w[2], &w[3], &w[4]); // ① 按指定格式扫入变量
// 干掉胡乱输入的,比如 33.3423.23423ASFDSDF.sdfsdf
char str[100];
sprintf(str, "%d.%d.%d.%d:%d", w[0], w[1], w[2], w[3], w[4]); // ② 将变量还原拼接成字符串
if (str != addr) return false; // 字符数组可以直接与string对比!
for (int i = 0; i < 5; i++) {
if (w[i] < 0) return false;
if (i < 4 && w[i] > 255) return false;
if (i == 4 && w[i] > 65535) return false;
}
return true;
}
int main() {
int n;
cin >> n;
unordered_map<string, int> hash;
for (int i = 1; i <= n; i++) {
string type, addr;
cin >> type >> addr;
if (type == "Server") {
if (!check(addr))
puts("ERR");
else if (hash.count(addr))
puts("FAIL");
else {
hash[addr] = i;
puts("OK");
}
} else {
if (!check(addr))
puts("ERR");
else if (!hash.count(addr))
puts("FAIL");
else
cout << hash[addr] << endl;
}
}
return 0;
}