5分WA求助
查看原帖
5分WA求助
1016101
xiaobeichen楼主2024/12/13 13:19

RT.

代码如下

#include<bits/stdc++.h>
#define LL long long
using namespace std;
vector<pair<int,string>>sv;
int turn(string str){//string转int 
	int v=1,sum=0;
	stack<char>num;//存储每一位数 
	for(int c:str){//入栈 
		num.push(c-'0');
	}
	while(!num.empty()){//遍历 
		int w=num.top();
		num.pop();
		sum+=w*v;//当前数乘权值 
		v*=10;
	}
	return sum;
}
bool check(string str){
	string s[10];
	int cnt=1,cnt1=0,cnt2=0;//cnt为地址分段数(即将ip以'.'或':'为间隔分成cnt段) ,cnt1为'.'数量, cnt2为':'数量 
	for(int i=0;i<str.size();i++){
		if(str[i]=='.'){
			if(cnt2)return 0;//若前方出现过':' ,返回0 
			cnt1++;
			cnt++;
		}else if(str[i]==':'){
			if(cnt1!=3)return 0;//若前方没有3个'.' ,返回0 
			cnt2++;
			cnt++;
		}else s[cnt]+=str[i];
		
	}
	for(int i=1;i<cnt;i++){
		int num=turn(s[i]);
		if(cnt!=5)return 0;//若地址数量不等于五 
		if(!(1<=num&&num<=255))return 0;
		if(cnt1!=3||cnt2!=1)return 0;//若'.'数量不为三或':'数量不为1 
	}
	if(!(1<=turn(s[5])&&turn(s[5])<=65535))return 0;
	return 1;
}
int find(string str){//在Servers中寻找地址为str的Server 
	for(pair<int,string> w:sv){
		if(w.second==str){//找到地址 
			return w.first;//返回下标 
		}
	}
	return 0;//不存在,返回特殊值0(地址从1开始) 
}
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		string op,ad;
		cin>>op>>ad;
		if(!check(ad)){
			cout<<"ERR"<<endl;
			continue;
		}
		if(op=="Server"){
			if(find(ad)){//巧妙利用整数转布尔值非0值为1的特性 
				cout<<"FAIL"<<endl;//服务器存在,连接失败 
			}else{
				cout<<"OK"<<endl;
				sv.push_back({i,ad});//服务器连接成功
			}
		}else{
			int get=find(ad);//sv中地址为ad的Server的数量 
			if(get){//若该Server存在 
				cout<<get<<endl;
			}else cout<<"FAIL"<<endl;//连接失败 
		}
	}
	/*string s;
	while(s!="end"){
		cin>>s;
		cout<<turn(s)<<endl;
		//调试代码,忽视 
	}*/
	return 0;
}
2024/12/13 13:19
加载中...