-
Notifications
You must be signed in to change notification settings - Fork 13
/
reversed-look-and-say.cpp
61 lines (47 loc) · 1.03 KB
/
reversed-look-and-say.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
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
string forward(const string& s)
{
ostringstream r;
for (size_t i = 0; i != s.length();)
{
auto i2 = s.find_first_not_of(s[i], i + 1);
if ( i2 == string::npos ) { i2 = s.length(); }
r << i2 - i << s[i];
i = i2;
}
return r.str();
}
bool backward(string& s)
{
string s2;
auto i1 = s.begin();
auto i2 = i1 + 1;
while ( i1 != s.end() && i2 != s.end() )
{
for(int i = 0; i < *i1-'0'; i++)
{
s2 += *i2;
}
i1 += 2;
i2 = i1 + 1;
}
if ( s.compare(forward(s2)) != 0 ) { return true; }
else if ( ( s2.size() % 2 == 1 ) || ( s2.compare(s) == 0 ) ) {s = s2;return true; }
s = s2;
return false;
}
int main()
{
string s;
getline(cin, s);
bool stop = false;
while ( !stop )
{
stop = backward(s);
}
cout << s << endl;
}