2 条题解
- 
  0
#include <iostream> #include <string> #include <map> using namespace std; map<string,int>m_cnt; bool check(string ip){ int a=-1,b=-1,c=-1,d=-1,e=-1; sscanf(ip.c_str(),"%d.%d.%d.%d:%d",&a,&b,&c,&d,&e); if(a<0||a>255||b<0||b>255||c<0||c>255||d<0||d>255||e<0||e>65535){ return false; } string t=to_string(a)+'.'+to_string(b)+'.'+to_string(c)+'.'+to_string(d)+':'+to_string(e); if(t==ip)return true; return false; } int main(){ string prestr,ip; int n; cin>>n; for(int i=1;i<=n;i++){ cin>>prestr>>ip; if(!check(ip)){ cout<<"ERR"<<endl; } else { if(prestr[0]=='S'){ if(m_cnt[ip]>0){ cout<<"FAIL"<<endl; } else{ cout<<"OK"<<endl; m_cnt[ip]=i; } } else{ if(m_cnt[ip]>0){ cout<<m_cnt[ip]<<endl; } else{ cout<<"FAIL"<<endl; } } } } } - 
  0
各位,这题比较容易,也就耗了我个小时

为什么错呢?
没开!!
因为问题失去小时生命,我觉得连人生哲理我都感悟出来了。
进入正题。
首先,输入一个字符串。
- 若为
Server,输入地址,不合法直接输出ERR,否则用一个查找,若曾经没创建过,非常好输出OK,储存,否则输出FAIL。 Client类似,输入地址,不合法直接输出ERR,否则,若曾经存过该地址,非常好输出序号,否则输出FAIL。
重点是判断是否合法的地方。
一个地址:
192.168.0.255:80挨个遍历。
- 遇到数字,用存。没啥好弄的。
 - 遇到符号,判断数字在不在范围内。
 - 遍历完后,再判断一次数字是否在范围内。
 
code:
#include <bits/stdc++.h> using namespace std; long long n; string o,s; map <string,long long> m; bool hf(string x) { long long num = 0,cnt = 0,cnt1 = 0,cnt2 = 0;//储存数字、'.'号个数、':'号个数、数字个数 for(long long i = 0;i < x.size();i++) { if(x[i] != ':' && x[i] != '.' && !(x[i] >= '0' && x[i] <= '9')) return false;//啥也不是 if((i == 0 || x[i - 1] == '.' || x[i - 1] == ':') && x[i] >= '0' && x[i] <= '9') cnt2++;//是数字 if(x[i] >= '0' && x[i] <= '9') { if(num == 0 && x[i] == '0' && i + 1 < x.size() && (x[i + 1] >= '0' && x[i + 1] <= '9')) return false;//有前导0 num = num * 10 + int(x[i] - '0');//存数字 } else if(x[i] == '.') { if(!(num >= 0 && num <= 255)) return false;//检查储存的数字 num = 0;//置0 cnt++;//'.'符号++ if(cnt > 3) return false;//若大于3个'.'符号,不行了 if(!cnt2) return false;//没有数字,又不行了 } else if(x[i] == ':') { if(!(num >= 0 && num <= 255)) return false;//同上,下面也是(作者真懒) num = 0; cnt1++; if(cnt1 > 1) return false; if(cnt < 3 || cnt2 < 4 || !cnt2) return false; } } if(!(num >= 0 && num <= 65535)) return false;//检查最后一个数字 if(cnt != 3 || cnt1 != 1 || cnt2 != 5) return false;//检查个数 return true; } signed main() { freopen("network.in","r",stdin); freopen("network.out","w",stdout); scanf("%lld",&n); for(long long i = 1;i <= n;i++) { cin >> o >> s; if(o == "Server") { if(hf(s)) { if(!m[s])//没创建过 { printf("OK\n"); m[s] = i; } else printf("FAIL\n"); } else { printf("ERR\n"); } } else { if(hf(s)) { if(!m[s])//找不到地址 { printf("FAIL\n"); } else printf("%lld\n",m[s]); } else { printf("ERR\n"); } } } return 0; } - 若为
 
- 1
 
信息
- ID
 - 1477
 - 时间
 - 1000ms
 - 内存
 - 256MiB
 - 难度
 - 7
 - 标签
 - 递交数
 - 32
 - 已通过
 - 9
 - 上传者