-
Notifications
You must be signed in to change notification settings - Fork 1
/
2001 S5 - Post's Correspondence Problem.cpp
96 lines (74 loc) · 2.22 KB
/
2001 S5 - Post's Correspondence Problem.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
87
88
89
90
91
92
93
94
95
96
/*
2001 S5 - Post's Correspondence Problem
Difficulty: Easy
Just uh try every single set of numbers
This works since m * n is no greater than like 40
You can essentially view this problem as a really big choice tree
We will perform recursive dfs on this tree
We will also use a stack to keep track of our current path
Since we need to print out the set in order
*/
#include <iostream>
#include <string>
#include <stack>
bool possible = false; //mark if solvable or not
std::stack<int> path;
int m, n;
std::string A [40];
std::string B [40];
//Since k cannot be greater than m, we need to keep track of k's value in each recurse
//solve(current value of k, which index from the array we want to add to our string, string a, string b)
void solve (int k, int i, std::string a, std::string b){
//If we're out of bounds, return nothing
if (k >= m){
return;
}
//Update strings, add current node to our path
path.push(i + 1);
a += A[i];
b += B[i];
//If they're the same
if (a == b){
possible = true;
//Output total terms used
std::cout << k << '\n';
std::stack<int> copy; //Since we put stuff in a stack the order is actually reverse
//Therefore we just flip the stack, by putting it in another stack. Master level programming right here
while (!path.empty()){
copy.push(path.top());
path.pop();
}
while (!copy.empty()){
std::cout << copy.top() << '\n';
copy.pop();
}
return;
}
//Try every possibility
for (int j = 0; j < n; j++){
solve(k + 1, j, a, b);
//If the stack is empty after a recurse, that means that we solved it since thats the only way the stack can become empty
if (path.empty()){
return;
}
}
//Remember to delete nodes if they're not necessary
path.pop();
}
int main(){
std::cin >> m >> n;
for (int i = 0; i < n; i++){
std::cin >> A[i];
}
for (int i = 0; i < n; i++){
std::cin >> B[i];
}
for (int i = 0; i < n; i++){
solve(1, i, "", "");
}
//If not solvable
if (!possible){
std::cout << "No solution.\n";
}
return 0;
}