【CSP-J 2021】网络连接 解题报告
1 题目链接
2 题目大意
题目:网络连接
题目大意:
(我有点懒,可不可以不写)
判断每一个服务机与客户机的情况。
3 解法分析
巨模拟。
O
(
n
∣
s
∣
log
n
)
O(n|s|\log n)
O(n∣s∣logn)
显然我们需要写一个函数 (我们机房一个大奆不屑于写函数) 。
- 首先判定地址串是否合法。
- 对于合法的地址串,判定机器类型。
- 开一个
map <string, int>
用于表示该地址串的情况(存在或不存在)。 - 输出即可。
本题目最难点在于如何判定地址串是否合法。
(但这毕竟只是J组的题)
详细见代码。
4 AC Code
#include <iostream>
#include <map>
#define int long long
using namespace std;
bool judge(string s) {
int pos[1007], poss;
int a, b, c, d, e;
int cnt = 0, cnt1 = 0;
for (int i = 0; i < s.size(); ++i)
if (s[i] == '.')
pos[++cnt] = i;
else if (s[i] == ':') {
poss = i;
++cnt1;
}
if (cnt != 3/*.的个数*/ || pos[3] > poss/*最后一个.在:后面*/ || cnt1 != 1/*含有多个:*/)
return 0;
if (pos[1] == 0 || pos[2] - pos[1] == 1 || pos[3] - pos[2] == 1
|| poss - pos[3] == 1 || s.size() - poss == 1)
return 0;//含有相邻的.或:
a = b = c = d = e = 0;
for (int i = 0; i < pos[1]; ++i) {
a *= 10;
a += s[i] - '0';
}//存入a 下同
if (s[0] == '0' && pos[1] != 1)
return 0;//判断是否有前导零 下同
if (!(a >= 0 && a <= 255))
return 0;//a的大小是否符合规则 下同
for (int i = pos[1] + 1; i < pos[2]; ++i) {
b *= 10;
b += s[i] - '0';
}
if (s[pos[1] + 1] == '0' && pos[2] - pos[1] != 2)
return 0;
if (!(b >= 0 && b <= 255))
return 0;
for (int i = pos[2] + 1; i < pos[3]; ++i) {
c *= 10;
c += s[i] - '0';
}
if (s[pos[2] + 1] == '0' && pos[3] - pos[2] != 2)
return 0;
if (!(c >= 0 && c <= 255))
return 0;
for (int i = pos[3] + 1; i < poss; ++i) {
d *= 10;
d += s[i] - '0';
}
if (s[pos[3] + 1] == '0' && poss - pos[3] != 2)
return 0;
if (!(d >= 0 && d <= 255))
return 0;
for (int i = poss + 1; i < s.size(); ++i) {
e *= 10;
e += s[i] - '0';
}
if (s[poss + 1] == '0' && s.size() - poss != 2)
return 0;
if (!(e >= 0 && e <= 65535))
return 0;
return 1;
}
int n;
string nm, ip;
map <string, int> mp;
signed main() {
scanf("%lld", &n);
for (int i = 1; i <= n; ++i) {
cin >> nm >> ip;
if (!judge(ip)) {
puts("ERR");
continue;
}//是否合法
if (nm == "Server") {
if (mp[ip]) {
puts("FAIL");
continue;
}//是否已被占用
puts("OK");
mp[ip] = i;
}
else if (nm == "Client") {
if (mp[ip] == 0) {
puts("FAIL");
continue;
}//是否存在
printf("%lld\n", mp[ip]);
}
else
puts("-1");
}
return 0;
}
5 胡言乱语
(蒟蒻昨天刚打完模拟赛今天就来水题解了)
祝:各位CSP考生AK(不会的暴力也能打满)!