'commit'
This commit is contained in:
53
TangDou/CSP-J/AcWingTraining/T1/4088.cpp
Normal file
53
TangDou/CSP-J/AcWingTraining/T1/4088.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user