-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathD4.cpp
More file actions
62 lines (58 loc) · 1.25 KB
/
Copy pathD4.cpp
File metadata and controls
62 lines (58 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
ifstream fin("input.txt");
ofstream fout("output.txt");
set <string> permutations;
void check_ip(string ip) {
vector <string> ip_numbers;
string number = "";
short count_true_check = 0;
for (char digit : ip) {
if (digit != '.') {
number += digit;
}
else {
if (number.size() >= 1) {
ip_numbers.push_back(number);
number = "";
}
}
}
if (!number.empty()) {
ip_numbers.push_back(number);
}
for (string number : ip_numbers) {
//cout << number << ' ';
if ((stoi(number) < 256) and ((number.size() > 1 and number[0] != '0') or (number.size() == 1))) {
count_true_check += 1;
}
}
//cout << '\n' << ip << '\n';
if (count_true_check == 4) {
permutations.insert(ip);
}
}
void permute(vector <string> arr, short l, short r, string ip) {
if (l == r) {
check_ip(ip);
}
else {
for (int i = l; i < r; i++) {
swap(arr[i], arr[l]);
permute(arr, l + 1, r, ip + arr[l]);
swap(arr[i], arr[l]);
}
}
}
int main() {
vector <string> inarr(4);
string ip_char;
for (int i = 0; i < 4; i++) {
fin >> ip_char;
inarr[i] = ip_char;
}
permute(inarr, 0, 4, "");
for (auto const ip: permutations) {
fout << ip << '\n';
}
}