没有优化,但是AC100%题解有些通不过的,要注意每个是否有数字,或者像01这种不合法的

#include <algorithm>
#include <sstream>
#include <vector>

using namespace std;

int main() {
    string str;
    while (getline(cin, str)) {
        istringstream ss;
        ss.str(str);
        string temp;
        vector<string> strv;
        while (getline(ss, temp, '.')) strv.push_back(temp);
        int i = 0;
        for (i = 0; i < strv.size(); i++) {
            if(strv.size()>4)break;
            if (strv[i].empty())break;
            bool digit =false;
            for(int j=0;j<strv[i].size();j++){
                if(!isdigit(strv[i][j])) {digit =false;break;}
                digit=true;
            }
            if(!digit)break;
            if(strv[i].size()>1&&strv[i][0]=='0')break;
            int x = stoi(strv[i]);
            if (x >= 0 && x <= 255) continue;
            else break;
        }
        if (i < 4) cout << "NO" << endl;
        else cout << "YES" << endl;

    }
}