2 条题解

  • 1
    @ 2022-7-19 21:35:31
    #include <bits/stdc++.h>
    using namespace std;
    int n;
    bool Check(string s) {
      long long a, b, c, d, port;
      if (sscanf(s.c_str(), "%lld.%lld.%lld.%lld:%lld", &a, &b, &c, &d, &port) != 5)  return false;
      if (a < 0 || a > 255 || b < 0 || b > 255 || c < 0 || c > 255 || d < 0 || d > 255 || port < 0 || port > 65535)  return false;
      stringstream ss;
      ss << a << '.' << b << '.' << c << '.' << d << ':' << port;
      return ss.str() == s;
    }
    map<string, int> mp;
    string op, ad;
    int main(int argc, char const *argv[]) {
      cin >> n;
      for (int i = 1; i <= n; i++) {
        cin >> op >> ad;
        if (!Check(ad)) { cout << "ERR\n"; continue; }
        if (op[0] == 'S') {
          if (mp[ad]) cout << "FAIL\n";
          else mp[ad] = i, cout << "OK\n";
        } else {
          if (!mp.count(ad)) cout << "FAIL\n";
          else cout << mp[ad] << '\n';
        }
      }
      return 0;
    }
    
    • 0
      @ 2021-10-27 19:14:06

      详见注释。

      #include <bits/stdc++.h>
      #define int long long
      #define ck(_T) (0<=(_T)&&(_T)<256)
      using namespace std;
      using tup=pair<bool,tuple<int,int,int,int,int>>;
      //pair < first: type, second: address >
      int n,aa,b,c,d,e;
      string type,address;
      tup a[1001];
      // check if x is a correct address (a.b.c.d:e)
      bool correct(string x)
      {
      	int dot=0,colon=0;// the number of '.' and ':'
      	// check the '.' and ':''s number
      	for(char cc:x)
      	{
      		if(cc=='.')dot++;
      		else if(cc==':') colon++;
      	}
      	if(dot!=3||colon!=1)
      	{
      //		cerr << "(stderr) Out at 1" << endl;
      		return 0;
      	}
      	// check if the order of 3 '.' and 1 ':' (correct: ...:)
      	bool flag=0;// if ':' appear, it will be 1
      	for(char cc:x)
      	{
      		if(cc==':')flag=1;
      		if(flag&&cc=='.')
      		{
      //			cerr << "(stderr) Out at 2" << endl;
      			return 0;
      		}
      	}
      	// check if the first character is '.'
      	if(x[0]=='.')
      	{
      //		cerr << "(stderr) Out at 3" << endl;
      		return 0;
      	}
      	// check if the last character is ':'
      	if(x[x.size()-1]==':')
      	{
      //		cerr << "(stderr) Out at 4" << endl;
      		return 0;
      	}
      	// check if two '.'(':') is near (like ".." or ".:")
      	for(int i=1;i<x.size();i++)
      		if(x[i-1]=='.'&&x[i]=='.'||x[i-1]=='.'&&x[i]==':')
      		{
      //			cerr << "(stderr) Out at 5" << endl;
      			return 0;
      		}
      	// check if has first-0
      	for(int i=2;i<x.size();i++)
      		if(!isdigit(x[i-2])&&x[i-1]=='0'&&isdigit(x[i]))
      		{
      //			cerr << "(stderr) Out at 6" << endl;
      			return 0;
      		}
      	// check 5 numbers' size
      	sscanf(address.c_str(),"%lld.%lld.%lld.%lld:%lld",&aa,&b,&c,&d,&e);
      //	cerr << "(stderr) " << a << " " << b << " " << c << " " << d << " " << e << endl; 
      	if(!(ck(aa)&&ck(b)&&ck(c)&&ck(d)&&0<=e&&e<65536))
      	{
      //		cerr << "(stderr) Out at 7" << endl;
      		return 0;
      	}
      	return 1;
      }
      signed main()
      {
      	freopen("network.in","r",stdin);
      	freopen("network.out","w",stdout);
      	cin >> n;
      	for(int i=1;i<=n;i++)
      	{
      		bool server=0,client=0;
      		// server: if server is failed, it is 1
      		// client: if client is connected, it is 1
      		cin >> type >> address;
      		if(!correct(address))
      		{
      			cout << "ERR" << endl;
      			goto end;
      		}
      //		sscanf(address.c_str(),"%d%d%d%d%d",&aa,&b,&c,&d,&e);
      		a[i]=make_pair(type=="Client",make_tuple(aa,b,c,d,e));
      		for(int j=1;j<i;j++)
      		{
      			if(!a[i].first&&a[j]==a[i])
      			{
      				cout << "FAIL" << endl;
      				server=1;
      				goto end;
      			}
      			// i:Server j:Server i.address==j.address -> FAIL
      			if(a[i].first&&!a[j].first&&a[j].second==a[i].second)
      			{
      				cout << j << endl;
      				client=1;
      				goto end;
      			}
      			// i:Client j:Server i.address==j.address -> Client-i connect to Server-j
      		}
      		if(!server&&!a[i].first)cout << "OK" << endl;
      		// if server isn't failed, puts OK
      		else if(!client&&a[i].first)cout << "FAIL" << endl;
      		// if client isn't connected, puts FAIL
      end:;	// this is the end for each computer.
      	} 
      	return 0;
      }
      
      • 1

      信息

      ID
      194
      时间
      1000ms
      内存
      512MiB
      难度
      8
      标签
      递交数
      7780
      已通过
      1104
      上传者