信奥赛普及组 2011 CSP-JP7911 网络连接 样例代码
#include <iostream>
#include <string.h>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
vector<string> split(string s, char c){
vector<string> res;
string ts;
for(int i=0;i<s.size();i++){
if(s[i]!=c){
ts+=s[i];
}else{
res.push_back(ts);
ts="";
}
}
res.push_back(ts);
return res;
}
bool all_digit(string s){
for(int i=0;i<s.size();i++){
if(!(s[i]>='0'&& s[i]<='9')){
return false;
}
}
return true;
}
bool lexsplit(string s){
vector<string> vs=split(s,':'),fvs;
if(vs.size()!=2){
return false;
}
fvs=split(vs[0],'.');
if(fvs.size()!=4){
return false;
}
for(auto ts : fvs){
if(ts.size()==0 || !all_digit(ts)){
return false;
}
ll tl=stoll(ts);
if(tl<0||tl>255){
return false;
}
if(ts[0]=='0' && ts.size()>1){
return false;
}
}
if(vs[1].size()==0 || !all_digit(vs[1])){
return false;
}
ll tl=stoll(vs[1]);
if(tl<0||tl>65535){
return false;
}
if(vs[1][0]=='0' && vs[1].size()>1){
return false;
}
return true;
}
bool lex(string s){
ll d,p,i=3;
char s1[256],s2[256];
for(int i=0;i<s.size();i++){
s1[i]=s[i];
}
s1[s.size()]='\0';
//strcpy(s1,s);
while(i>0){
if(sscanf(s1,"%lld.%s",&d,s2)!=2){
return false;
}
if(s1[0]=='0' && d>0 || d<0 || d>255 || s1[0]=='0' && s1[1]=='0'){
return false;
}
i--;
strcpy(s1,s2);
}
if(sscanf(s1,"%lld:%s",&d,s2)!=2){
return false;
}
//cout<<s2<<" "<<d<<" "<<p<<endl;
if(s1[0]=='0' && d>0 || d<0 || d>255 || s1[0]=='0' && s1[1]=='0'){
return false;
}
strcpy(s1,s2);
if(sscanf(s1,"%lld%s",&p,s2)!=1){
return false;
}
if(p<0 || p>65535 || strlen(s1)>1 && p==0 || s1[0]=='0' && p>0){
//cout<<strlen(s1)<<endl;
return false;
}
return true;
}
int main(){
int n;
string s,name;
cin>>n;
map<string,int> ss;
for(int i=1;i<=n;i++){
cin>>name>>s;
//if(lex(s)){
if(lexsplit(s)){
if(ss.count(s)>0){
if(name=="Server"){
cout<<"FAIL"<<endl;
}else{
cout<<ss[s]<<endl;
}
}else{
if(name=="Server"){
cout<<"OK"<<endl;
ss[s]=i;
}else{
cout<<"FAIL"<<endl;
}
}
}else{
cout<<"ERR"<<endl;
}
}
return 0;
}