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

57 lines
2.1 KiB
C++
Raw Permalink 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 ip) {
int t[6] = {0, -1, -1, -1, -1, -1}; // 用来把ip里的数字读出
int cnt = sscanf(ip.c_str(), "%d.%d.%d.%d:%d", &t[1], &t[2], &t[3], &t[4], &t[5]); // cnt存ip里有多少个数据
// 好了,后(划掉)先进的sscanf来啦~
// ip.c_str() 比较先进的东东,把ip转换为char类型
//"%d.%d.%d.%d:%d" 跳过.和:,把数据读出
//&t[1],&t[2],&t[3],&t[4],&t[5] 把读出的数据填到t数组里
if (cnt != 5)
return false; // 如果cnt不等于5,说明数据个数不对,返回不合法
for (int i = 1; i <= 4; i++) { // 判断数据有没有超过上限
if (t[i] < 0 || t[i] > 255)
return false;
}
if (t[5] < 0 || t[5] > 65535)
return false; // 判断数据有没有超过上限
char now[60]; // 用来把数字读回,从而卡掉前导0和符号错误
sprintf(now, "%d.%d.%d.%d:%d", t[1], t[2], t[3], t[4], t[5]); // 又是一个先进的东东
// 和前面差不多
// now 前面那个用来把数字读回,从而卡掉前导0和符号错误的数组
//"%d.%d.%d.%d:%d" 和sscanf效果差不多,只是把数据读回
// t[1],t[2],t[3],t[4],t[5] 和sscanf效果差不多,依次填入%d里
if (now != ip)
return false; // 如果now不等于原来的ip,说明有前导0或符号错误,返回不合法
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;
}