80分求助
查看原帖
80分求助
528287
多喝岩浆楼主2022/4/11 15:18
#include<bits/stdc++.h>

using namespace std;
const int N = 1e3 + 10;
int n;
map <string, int> Service_machine;//服务机
bool check (string checker) {
	int Number_of_dot = 0, Number_of_colon = 0;//点,冒号的数量
	int length = checker.size ();//长度
	for (int i = 0; i < length; i++) {
		if (checker[i] == '.') Number_of_dot++;
		if (checker[i] == ':') Number_of_colon++;
		if (Number_of_dot < 3 && Number_of_colon >= 1) return false;//说明不符合,早了。a.b:c.d.e 
	} 
	if (Number_of_dot != 3 || Number_of_colon != 1) return false;//说明不符合,达不到a.b.c.d:e
	int ans[10], subscript = 1; memset (ans, 0, sizeof (ans));//用于存储abcde,subscript是下标;
	for (int i = 0; i < length; i++) //记录abcde 
		if (checker[i] == '.' || checker[i] == ':') subscript++;
		else ans[subscript] = ans[subscript] * 10 + checker[i] - '0';
	if (subscript != 5) return false;
	for (int i = 1; i < 5; i++) if (!(ans[i] >= 0 && ans[i] <= 255)) return false;//是否在范围内 
	if (!(ans[5] >= 0 && ans[5] <= 65535)) return false;//是否在范围内
	int The_distance_between_points[10], len = 1, last = 0;//点与点之间的距离 
	for (int i = 0; i < length; i++) //计算点与点之间的距离 
		if (checker[i] == '.' || checker[i] == ':') { 
			The_distance_between_points[len] = i - 1 - (last + 1) + 1;//计算点与点之间的距离 
			last = i;
			len++;
		}
	The_distance_between_points[5] = length - 1 - (last + 1) + 1;//计算点与点之间的距离 
	for (int i = 0; i < length; i++) 
		if (checker[i] == '.') {
			The_distance_between_points[1] = i;//计算点与点之间的距离 
			break;
		} 
	if (checker[0] == '0' && The_distance_between_points[1] >= 2) return false;
	int pre = 2;//下标 
	for (int i = 0; i < length; i++)//如果第一位是0且长度大于2说明有前导0,直接返回false 
		if (checker[i] == '.' || checker[i] == ':')
			if (The_distance_between_points[pre] >= 2 && checker[i + 1] == '0') return false;
			else pre++;
	return true;//判断完了,如果还没被筛掉,说明符合 
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);//快读快写 
	cin >> n;
	for (int i = 1; i <= n; i++) {
		string type, num;
		cin >> type >> num;
		if (type == "Server") { // 服务机 
			if (check (num)) {
				if (Service_machine[num]) cout << "FAIL" << endl;//符合,但已经存在
				else if (Service_machine[num] == 0) cout << "OK" << endl, Service_machine[num] = i;//成功加入
			}
			else cout << "ERR" << endl;//不符合 
		}
		else if (type == "Client") {
			if (check (num)) {
				if (Service_machine[num]) cout << Service_machine[num] << endl;//符合,存在,输出编号
				else if (Service_machine[num] == 0) cout << "FAIL" << endl;//虽然符合,但找不到服务机 
			}
			else cout << "ERR" << endl;//不符合 
		}
	}
	return 0;
}
2022/4/11 15:18
加载中...