提交时间:2023-11-19 18:17:29
运行 ID: 111307
#include <bits/stdc++.h> using namespace std; #define all(v) (v).begin(), (v).end() typedef long long ll; int main() { string ip; cin >> ip; // print all the possible ip addresses // no leading zero except 0 // 0~255 // 1. brute force int n = ip.size(); for(int i = 1; i < n; i++) { for(int j = i+1; j < n; j++) { for(int k = j+1; k < n; k++) { string a = ip.substr(0,i); string b = ip.substr(i,j-i); string c = ip.substr(j,k-j); string d = ip.substr(k,n-k); if(a.size() > 1 && a[0] == '0') continue; if(b.size() > 1 && b[0] == '0') continue; if(c.size() > 1 && c[0] == '0') continue; if(d.size() > 1 && d[0] == '0') continue; int A = stoi(a), B = stoi(b), C = stoi(c), D = stoi(d); if(A <= 255 && B <= 255 && C <= 255 && D <= 255) { cout << A << '.' << B << '.' << C << '.' << D << '\n'; } } } } return 0; }