-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion_8.cpp
More file actions
60 lines (51 loc) · 1.31 KB
/
question_8.cpp
File metadata and controls
60 lines (51 loc) · 1.31 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
void genIp2(string& s, int idx, int count, vector<string>& result, string current) {
if (count == 4 && idx == s.size()) {
current.pop_back();
result.push_back(current);
return;
}
if (count == 4 || idx == s.size()) {
return;
}
for (int i = 1; i <= 3; i++) {
if (idx + i > s.size()) {
break;
}
string part = s.substr(idx, i);
int val = stoi(part);
if (val > 255 || (i > 1 && s[idx] == '0')) {
continue;
}
genIp2(s, idx + i, count + 1, result, current + part + '.');
}
}
vector<string> genIp(string& s) {
vector<string> result;
string current;
genIp2(s, 0, 0, result, current);
return result;
}
};
int main() {
int T = 1;
while (T--) {
string s = "25525511135";
Solution obj;
vector<string> str = obj.genIp(s);
sort(str.begin(), str.end());
if (str.size() == 0)
cout << -1 << "\n";
else {
for (auto& u : str) {
cout << u << "\n";
}
}
}
return 0;
}