Advertisement
SaberToaster

https://code.hithaui.com/problem/F_HCS2022_KC_3

Aug 17th, 2022
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. vector<string> ans;
  4. int is_valid(string ip)
  5. {
  6.     vector<string> ips;
  7.     string ex = "";
  8.     for (int i = 0; i < ip.size(); i++)
  9.     {
  10.         if (ip[i] == '.')
  11.         {
  12.             ips.push_back(ex);
  13.             ex = "";
  14.         }
  15.         else
  16.         {
  17.             ex = ex + ip[i];
  18.         }
  19.     }
  20.     ips.push_back(ex);
  21.     for (int i = 0; i < ips.size(); i++)
  22.     {
  23.  
  24.         if (ips[i].length() > 3 || stoi(ips[i]) < 0 || stoi(ips[i]) > 255)
  25.             return 0;
  26.  
  27.         if (ips[i].length() > 1 && stoi(ips[i]) == 0)
  28.             return 0;
  29.  
  30.         if (ips[i].length() > 1 && stoi(ips[i]) != 0 && ips[i][0] == '0')
  31.             return 0;
  32.     }
  33.     return 1;
  34. }
  35.  
  36. void convert(string ip)
  37. {
  38.     int l = ip.length();
  39.  
  40.     if (l > 12 || l < 4)
  41.     {
  42.     }
  43.  
  44.     string check = ip;
  45.  
  46.     for (int i = 1; i < l - 2; i++)
  47.     {
  48.         for (int j = i + 1; j < l - 1; j++)
  49.         {
  50.             for (int k = j + 1; k < l; k++)
  51.             {
  52.                 check = check.substr(0, k) + "." + check.substr(k);
  53.                 check = check.substr(0, j) + "." + check.substr(j);
  54.                 check = check.substr(0, i) + "." + check.substr(i);
  55.  
  56.                 if (is_valid(check))
  57.                 {
  58.                     ans.push_back(check);
  59.                     // cout << check << '\n';
  60.                 }
  61.                 check = ip;
  62.             }
  63.         }
  64.     }
  65. }
  66.  
  67. int main()
  68. {
  69.     string A;
  70.     string temp = "";
  71.     int index = 0;
  72.     cin >> A;
  73.     convert(A);
  74.     cout << ans.size() << '\n';
  75.     for (int i = 0; i < (int)ans.size(); i++)
  76.         cout << ans.at(i) << endl;
  77.     return 0;
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement