-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolding_machine.cpp
80 lines (65 loc) · 1.93 KB
/
folding_machine.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
#include <bits/stdc++.h>
using namespace std;
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define f first
#define s second
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const int INF = 0x3f3f3f3f;
const int MAX = 20;
ofstream file("output.txt");
char ans;
map<vector<int>, int> memo;
bool check_fold(vector<int> &v, vector<int> &out){
if(v.size() != out.size()) return false;
int i = 0;
while(v[i] == out[i] and i < out.size()) i++;
if(i == v.size()) return true;
return false;
}
vector<int> gen_fold(vector<int> &v, int cut){
vector<int> new_v;
list<int> left, right;
for(int i = 0; i < v.size(); i++){
if(i < cut) left.push_front(v[i]);
else right.push_back(v[i]);
}
std::list<int>::iterator it_l = left.begin(), it_r = right.begin();
while(it_l != left.end() and it_r != right.end()){
new_v.push_back(*it_l + *it_r);
it_l++, it_r++;
}
while(it_l != left.end()) new_v.push_back(*it_l), it_l++;
while(it_r != right.end()) new_v.push_back(*it_r), it_r++;
return new_v;
}
void solve(vector<int> v, vector<int> &out){
if(v.size() < out.size()) return;
if(check_fold(v, out)){
ans = 'S';
return;
}
memo[v] = 1;
for(int i = 0; i <= v.size(); i++){
vector<int> n = gen_fold(v, i);
if(!memo[n]){
solve(n, out);
}
}
}
int main(){_
int sz_v, sz_o;
while(cin >> sz_v){
ans = 'N';
vector<int> v(sz_v);
for(int i = 0; i < sz_v; i++) cin >> v[i];
cin >> sz_o;
vector<int> out(sz_o);
for(int i = 0; i < sz_o; i++) cin >> out[i];
if(accumulate(v.begin(), v.end(), 0) == accumulate(out.begin(), out.end(), 0)){
solve(v, out);
}
memo.clear();
cout << ans << endl;
}
return 0;
}