-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.2.cpp
86 lines (80 loc) · 1.52 KB
/
11.2.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<iostream>
#include <string>
void incrementString(std::string&);
bool hasTwoPairs(const std::string&);
bool hasBadLetter(const std::string&);
bool HasSequence(const std::string&);
int main()
{
std::string password = "vzbxxyzz";
incrementString(password);
bool done = false;
do
{
if (hasTwoPairs(password) && !hasBadLetter(password) && HasSequence(password))
done = true;
else
incrementString(password);
} while (!done);
std::cout << password;
}
void incrementString(std::string& word)
{
for (auto it = word.rbegin(); it != word.rend(); ++it)
{
if (*it == 'z')
{
*it = 'a';
}
else
{
(*it)++;
break;
}
}
}
bool hasTwoPairs(const std::string& word)
{
char first = 0, previous = 0;
int counter = 0, pair_count = 0;
for (auto e : word)
{
if (e == previous)
{
counter++;
}
if (e != previous)
{
if (counter)
{
if (first == 0)
{
first = previous;
pair_count++;
}
else if (first != e && first != previous)
{
pair_count++;
}
counter = 0;
}
previous = e;
}
}
if (word[word.size() - 1] == word[word.size() - 2])
pair_count++;
return pair_count >= 2;
}
bool hasBadLetter(const std::string& word)
{
return word.find("iol", 0) != std::string::npos;
}
bool HasSequence(const std::string& word)
{
for (std::size_t i = 0; i < word.size() - 2; i++)
{
if (word[i] + 1 == word[i + 1] && word[i + 1] + 1 == word[i + 2])
return true;
}
return false;
}