题解 2A
- 对于前导零的判断:0也可以有前导零,即000也算出现了前导零
大模拟,不过长度为30的话许多人写的会爆long long,懒得卡了
Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100010
#define ll long long
#define reg register int
//#define int long long
int n;
char s[100];
int sta[100];
bool check() {
char* t=s;
int tem, len;
bool pre_zero;
for (int i=1; i<=3; ++i,++t) {
if (!isdigit(*t)) return 0;
tem=0, len=0;
if (*t=='0') pre_zero=1;
else pre_zero=0;
while (isdigit(*t)) {
tem=(tem<<3)+(tem<<1)+((*t)^48);
if (++len>3) return 0;
++t;
}
if (pre_zero&&len>1) return 0;
if (tem>255) return 0;
if (*t!='.') return 0;
}
if (!isdigit(*t)) return 0;
tem=0, len=0;
if (*t=='0') pre_zero=1;
else pre_zero=0;
while (t<s+n && isdigit(*t)) {
tem=(tem<<3)+(tem<<1)+((*t)^48);
if (++len>3) return 0;
++t;
}
if (pre_zero&&len>1) return 0;
if (tem>255) return 0;
if (t!=s+n) return 0;
return 1;
}
void read() {
char* t=s;
int tem; bool over;
for (int i=1; i<=4; ++i) {
tem=0, over=0;
while (!isdigit(*t)) ++t;
while (t<s+n && isdigit(*t)) {
tem=(tem<<3)+(tem<<1)+((*t)^48);
if (tem>255) over=1;
++t;
}
if (over || tem>255) sta[i]=255;
else sta[i]=tem;
}
}
signed main()
{
freopen("ip.in", "r", stdin);
freopen("ip.out", "w", stdout);
scanf("%s", s); n=strlen(s);
if (check()) puts("YES");
else {
puts("NO");
read();
printf("%d.%d.%d.%d\n", sta[1], sta[2], sta[3], sta[4]);
}
return 0;
}