题解 | #合法IP#

合法IP

http://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string str;
    cin>>str;
    regex reg("[^.]+");
    int iCount = 0;
    for(sregex_iterator i(str.begin(),str.end(),reg),end_i; i!=end_i; ++i)
    {
        string stp = i->str();
        if(stp.find('+') != -1 || stp.find('-') != -1 || (stp.length()!=1 && stp[0] == '0'))
        {       //有+ - 有开头无效0
            cout << "NO";
            return 0;
        }          
        stringstream ss; int itp;
        ss<<stp;  ss>>itp;
        if(itp>255)
        {
            cout << "NO";
            return 0;
        }   
        iCount++;
    }
    if(iCount == 4)
        cout << "YES";
    else cout << "NO";
    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务