题解 | #合法IP#
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
#include <cstdio> #include <iostream> #include <regex> #include <string> using namespace std; int main() { string input; cin >> input; string match = R"([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})"; regex reg_match(match); if(!regex_match(input, reg_match)) { puts("NO"); return 0; } string sep = R"([.])"; regex reg(sep); sregex_token_iterator iter(input.cbegin(), input.cend(), reg, -1); sregex_token_iterator end; for(; iter != end; ++ iter) { if(iter->str().length() > 1 && iter->str()[0] == '0') //前倒0 { puts("NO"); return 0; } if(stoi(iter->str()) > 255 || stoi(iter->str()) < 0) { puts("NO"); return 0; } } puts("YES"); return 0; return 0; } // 64 位输出请用 printf("%lld")