'commit'
This commit is contained in:
58
TangDou/CSP-J/AcWingTraining/T1/ip_check.cpp
Normal file
58
TangDou/CSP-J/AcWingTraining/T1/ip_check.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int w[5];
|
||||
bool check(string addr) {
|
||||
memset(w, -1, sizeof w);
|
||||
// 使用 stringstream 替代 sscanf
|
||||
stringstream ss(addr);
|
||||
char dot1, dot2, dot3, colon;
|
||||
ss >> w[0] >> dot1 >> w[1] >> dot2 >> w[2] >> dot3 >> w[3] >> colon >> w[4];
|
||||
|
||||
// 验证格式是否正确
|
||||
if (dot1 != '.' || dot2 != '.' || dot3 != '.' || colon != ':')
|
||||
return false;
|
||||
|
||||
// 验证数值范围
|
||||
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;
|
||||
}
|
||||
|
||||
// 验证转换后的字符串是否与原字符串相同
|
||||
stringstream ss2;
|
||||
ss2 << w[0] << '.' << w[1] << '.' << w[2] << '.' << w[3] << ':' << w[4];
|
||||
return ss2.str() == addr;
|
||||
}
|
||||
|
||||
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