本题直接模拟的话会十分复杂,C语言标准库中有两个针对于字符串固定格式输入输出的函数,
下图是给字符型数组ad按照固定格式输入的样例,在此之前,我们已经输入了ad数组,sscanf用于检测按照特定格式输入时成功输入的个数
int cnt=sscanf(ad,"%lld.%lld.%lld.%lld:%lld",&a,&b,&c,&d,&e);
1.2.3.4:5 会返回5
1:2:3:4:5 会返回1
下面是把若干数字转化为固定格式字符串的样例
sprintf(add,"%lld.%lld.%lld.%lld:%lld",a,b,c,d,e);
a,b,c,d,e取1,2,3,4,5时会将add数组赋值为1.2.3.4:5
#include <iostream>
# include<cstdio>
# include<map>
# include<string.h>
using namespace std;
typedef long long int ll;
map<string,int>m;
int main ()
{
int t;
cin>>t;
int id=0;
while(t--)
{
char op[1000],ad[1000];
cin>>op>>ad;
id++;
ll a=-1,b=-1,c=-1,d=-1,e=-1;
int cnt=sscanf(ad,"%lld.%lld.%lld.%lld:%lld",&a,&b,&c,&d,&e),flag=0;
if(cnt!=5)
flag=1;
char add[10000];
sprintf(add,"%lld.%lld.%lld.%lld:%lld",a,b,c,d,e);
int len1=strlen(ad),len2=strlen(add);
if(len1!=len2)
flag=1;
else
{
for(int i=0;i<len1;i++)
{
if(ad[i]!=add[i])
{
flag=1;
break;
}
}
}
if(a<0||a>255||b<0||b>255||c<0||c>255||d<0||d>255)
flag=1;
if(e>65535||e<0)
flag=1;
if(op[0]=='S')
{
string t(ad);
if(flag)
{
cout<<"ERR"<<'\n';
}
else if(m[t])
{
cout<<"FAIL"<<'\n';
}
else
{
cout<<"OK"<<'\n';
m[t]=id;
}
}
else
{
string t(ad);
if(flag)
{
cout<<"ERR"<<'\n';
}
else if(m[t]==0)
{
cout<<"FAIL"<<'\n';
}
else if(m[t])
{
cout<<m[t]<<'\n';
}
}
}
return 0;
}