-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1261.cpp
More file actions
66 lines (54 loc) · 1.23 KB
/
Copy path1261.cpp
File metadata and controls
66 lines (54 loc) · 1.23 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
63
64
65
66
#include <bits/stdc++.h>
using namespace std;
map<string, bool> seen;
bool solve(string str)
{
if (seen.find(str) != seen.end())
return seen[str];
int i = 0, j;
// for every element of the string
while (i < (int)str.size())
{
j = i;
// let j be the first index where str[j] != str[i]
while (j < (int)str.size() and str[j] == str[i])
j += 1;
// if str[i..j] is a block of at least 2 chars, it can be popped
// so we can recursively call the procedure for the remaining previous
// and next string
if (j - i >= 2)
{
string prev = str.substr(0, i);
string next = str.substr(j, string::npos);
bool res = solve(prev + next);
// is a solution is found for the remainder, then a solution for the
// entire initial string was found
if (res)
{
seen[str] = res;
return res;
}
}
// we only get here is solution was not found by popping the current block
// or if the block didn't have at least 2 chars;
// we go to the next possible block
i = j;
}
// if we get here, then no solution was found
seen[str] = false;
return false;
}
int main()
{
int T;
string str;
cin >> T;
while (T--)
{
seen.clear();
seen[""] = true;
cin >> str;
cout << solve(str) << "\n";
}
return 0;
}