Repository navigation
Expand file tree
/
Copy pathLR_String.cpp
More file actions
90 lines (86 loc) · 2.17 KB
/
Copy pathLR_String.cpp
File metadata and controls
90 lines (86 loc) · 2.17 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
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
void solve(){
string s;
cin>>s;
int q;
cin>>q;
vector<int> l;
vector<int> r;
vector<int> lr;
for (int i = 0;i<s.size();i++) {
if (s[i] == 'L') {
l.push_back(i);
}
if (s[i] == 'R') {
r.push_back(i);
}
if (i + 1 < s.size() && s[i] == 'L' && s[i+1] == 'R') {
lr.push_back(i);
}
}
while (q--) {
string t;
cin>>t;
bool ok =true;
if (t[0] == 'R' && s[0]!= 'R' || t[t.size()-1]=='L' && s[s.size()-1]!= 'L') {
cout<<"NO"<<endl;
continue;
}
int pos = 0;
for (int i = 0;i<t.size();i++) {
if (i == t.size() -1 ) {
if (t[i] == 'L') {
auto it = lower_bound(l.begin(), l.end(), pos);
if (it == l.end()) {
ok =false;
}
}else {
auto it = lower_bound(r.begin(), r.end(), pos);
if (it == r.end()) {
ok =false;
}
}
}
if (t[i]=='L'&&t[i+1]=='R') {
auto it = lower_bound(lr.begin(), lr.end(), pos);
if (it == lr.end()) {
ok =false;
break;
}
pos = *it + 2;
i++;
}else if (t[i] == 'R') {
auto it = lower_bound(r.begin(), r.end(), pos);
if (it == r.end()) {
ok = false;
break;
}
pos = *it +1;
}else {
auto it = lower_bound(l.begin(), l.end(), pos);
if(it == l.end()) {
ok =false;
break;
}
pos = *it + 1;
}
}
if (ok) {
cout<<"YES"<<endl;
}else {
cout<<"NO"<<endl;
}
}
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int T =1;
cin>>T;
while(T--){
solve();
}
return 0;
}