-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2021-BIO-1.cpp
More file actions
115 lines (100 loc) · 3.65 KB
/
2021-BIO-1.cpp
File metadata and controls
115 lines (100 loc) · 3.65 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Down Pat
#include <bits/stdc++.h>
using namespace std;
// 2023-11-11 Marking (1 right, 0 wrong, * bug):
// 11111111111 24/24
bool splitPatReversed(int pat[], int start, int end);
/**
* Return if the pat is valid (the pat is the subarray from `start` to `end`, `start < end`)
*/
bool splitPat(int pat[], int start, int end) {
if(start >= end-1) {
// cout << "Valid " << start << "\n";
return true;
}
// cout << "Testing " << start << " " << end << "\n";
// Find splits that follow left all greater than right rule
// Try for each split index (index of rightmost char of left side)
int leftMin = 26; // As alphabet 0 to 25
for(int splitI = start; splitI < end-1; splitI++) {
if(pat[splitI] < leftMin) { // TODO: Debug why split 3
leftMin = pat[splitI];
}
// must follow left all greater than right rule
bool valid = true;
for(int checkRightI = splitI+1; checkRightI < end; checkRightI++) {
if(pat[checkRightI] >= leftMin) {
// cout << "Wrong" << checkRightI << "min" << leftMin << "\n";
valid = false;
break;
}
}
if(valid) {
// Valid pat at this split
// cout << "Deeper " << start << " " << end << " Split@" << splitI+1 << "\n";
if (splitPatReversed(pat, splitI+1, start) && splitPatReversed(pat, end, splitI+1)) {
return true;
}
}
}
// cout << "Invalid " << start << " " << end << "\n";
return false;
}
/**
* Return if the pat is valid (the pat is the *reversed* subarray from `start` to `end`, `start > end`)
*/
bool splitPatReversed(int pat[], int start, int end) {
if(start <= end+1) {
// cout << "Valid " << start << "\n";
return true;
}
// cout << "Testing Reversed " << start << " " << end << "\n";
// Find splits that follow left all greater than right rule
// Try for each split index (index of rightmost char of left side)
int leftMin = 26; // As alphabet 0 to 25
for(int splitI = start-1; splitI > end; splitI--) {
if(pat[splitI] < leftMin) {
leftMin = pat[splitI];
}
// must follow left all greater than right rule
bool valid = true;
for(int checkRightI = splitI-1; checkRightI >= end; checkRightI--) {
if(pat[checkRightI] >= leftMin) {
// cout << "Wrong" << checkRightI << "min" << leftMin << "\n";
valid = false;
break;
}
}
if(valid) {
// Valid pat at this split
// cout << "Deeper Reversed " << start << " " << end << " Split@" << splitI << "\n";
if (splitPat(pat, splitI, start) && splitPat(pat, end, splitI)) {
return true;
}
}
}
// cout << "Invalid Reversed " << start << " " << end << "\n";
return false;
}
string bool2YesNo(bool b) {
return b ? "YES" : "NO";
}
int main() {
string s1_str, s2_str;
cin >> s1_str >> s2_str;
int s1_len = s1_str.size();
int s2_len = s2_str.size();
int s1PlusS2[s1_len + s2_len]; // Concatenated
for(int i = 0; i < s1_len; i++) {
s1PlusS2[i] = s1_str[i] - 'A'; // A0,B1,C2...
// cout << s1_str[i] - 'A' << "!";
}
for(int i = 0; i < s2_len; i++) {
s1PlusS2[s1_len+i] = s2_str[i] - 'A'; // A0,B1,C2...
// cout << s2_str[i] - 'A' << "!";
}
cout << bool2YesNo(splitPat(s1PlusS2, 0, s1_len)) << "\n";
cout << bool2YesNo(splitPat(s1PlusS2, s1_len, s1_len+s2_len)) << "\n";
cout << bool2YesNo(splitPat(s1PlusS2, 0, s1_len+s2_len)) << "\n";
return 0;
}