用暴力枚举做的大模拟
查看原帖
用暴力枚举做的大模拟
1017064
chenboye楼主2024/8/17 14:46

二话不说上代码

#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<cstdio>
using namespace std;
struct str{
	string op,ad;
	int id;
};
bool check(string s){//判断格式范围和前导0
	long long a,b,c,d,e;
	if(sscanf(s.c_str(),"%lld.%lld.%lld.%lld:%lld",&a,&b,&c,&d,&e)!=5){
		return false;
	}
	if(a<0||a>255||b<0||b>255||c<0||c>255||d<0||d>255||e<0||e>65535){
		return false;
	}
	stringstream oss;
	oss<<a<<'.'<<b<<'.'<<c<<'.'<<d<<':'<<e;
	return oss.str()==s;
}
int main(){
	int n;
	cin>>n;
	const int ss=n;
	str a[ss];
	for(int i=0;i<n;i++){
		int c=0;
		cin>>a[i].op>>a[i].ad;
		if(a[i].op=="Server"){
			a[i].id=i+1;
		}
	}
	for(int i=0;i<n;i++){
		if(!(check(a[i].ad))){
			cout<<"ERR"<<endl;
			continue;
		}
		if(a[i].op=="Server"){
			if(a[i].id==1){
				cout<<"OK"<<endl;
				continue;
			}
			else{
				bool n=false;
				for(int j=i-1;j>=0;j--){
					if(a[i].ad==a[j].ad&&a[j].op=="Server"){
						cout<<"FAIL"<<endl;
						n=true;
						a[i].op="SSS";
						break;
					}					
				}
				if(n==0){
					cout<<"OK"<<endl;
				}
			}
		}
		if(a[i].op=="Client"){
			bool n=false;
			for(int j=i;j>=0;j--){
				if(a[i].ad==a[j].ad&&a[j].op=="Server"){
					cout<<a[j].id<<endl;
					n=true;
					break;
				}
			}
			if(n==false){
				cout<<"FAIL"<<endl;
			}
		}
	}
	return 0;
}
2024/8/17 14:46
加载中...