75pts 求助
查看原帖
75pts 求助
1353330
K_J_M楼主2024/7/5 11:46
#include<bits/stdc++.h>
using namespace std;
int n;
struct node{
	string leixing;
	string adress;
}a[1010];
map<string,bool>vis;
map<string,int>id;
int get(string s,int l,int r){
	int sum=0;
	for(int i=l;i<=r;i++){
		sum=sum*10+s[i]-'0';
	}
	return sum;
}//求数字
bool OK(string s){
	for(int i=0;i<s.size();i++){
		if((s[i]>='0'&&s[i]<='9')||(s[i]=='.')||(s[i]==':')) continue;
		else return false;
	}
	int pos[1010]={0},cnt=0;
	for(int i=0;i<s.size();i++){
		if(s[i]=='.'){
			pos[++cnt]=i+1;
		}
	}
	if(cnt==3){
		int tot=0;
		for(int i=0;i<s.size();i++){
			if(s[i]==':') tot++;
		}
		if(tot==1){
			for(int i=0;i<s.size();i++){
				if(s[i]==':'){
					if((pos[cnt]>i+1)||(s.size()-1==i)){
						return false;
					}else{
						pos[++cnt]=i+1;
						break;
					}
				}
			}
		}else{
			return false;
		}
		for(int i=1;i<=cnt;i++){
			if(pos[i]-pos[i-1]<=1){
				return false;
			}
		}
		int a,b,c,d,e;
		if((s[0]=='0'&&pos[1]-1>=2)||(s[pos[1]]=='0'&&pos[2]-pos[1]-1>=2)||(s[pos[2]]=='0'&&pos[3]-pos[2]-1>=2)||(s[pos[3]]=='0'&&pos[4]-pos[3]-1>=2)||(s[pos[4]]=='0'&&s.size()-pos[4]>=2)) return false;
		a=get(s,0,pos[1]-2);
		b=get(s,pos[1],pos[2]-2);
		c=get(s,pos[2],pos[3]-2);
		d=get(s,pos[3],pos[4]-2);
		e=get(s,pos[4],s.size()-1);
		if(a>=0&&a<=255&&b>=0&&b<=255&&c>=0&&c<=255&&d>=0&&d<=255&&e>=0&&e<=65535) return true;
		else return false;
	}else{
		return false;
	}
}//判断地址是否合法
int main(){
	cin>>n;
	for(int o=1;o<=n;o++){
		cin>>a[o].leixing>>a[o].adress;
		if(a[o].leixing=="Server"){
			if(OK(a[o].adress)){
				if(vis[a[o].adress]){
					cout<<"FAIL"<<endl;
				}else{
					vis[a[o].adress]=true;
					id[a[o].adress]=o;
					cout<<"OK"<<endl;
				}
			}else{
				cout<<"ERR"<<endl;
			}
		}else{
			if(OK(a[o].adress)){
				if(vis[a[o].adress]){
				cout<<id[a[o].adress]<<endl;
				}else{
					cout<<"FAIL"<<endl;
				}
			}else{
				cout<<"ERR"<<endl;
			}
		}
	}
	return 0;
}
2024/7/5 11:46
加载中...