这道题涉及到两个部分,先是逆向思维,正着做一定会无比困难,而倒过来想就会好做,也比较难想到逆向思维,见识又少了,
倒着思考就得先找到最后一个移除的元素
include <bits/stdc++.h>
using namespace std;
define forn(i, n) for (int i = 0; i < int(n); i++)
int main() {
int t;
cin >> t;
forn(tt, t) {
int n, m;
cin >> n >> m;
vector
forn(i, n)
cin >> a[i];
string s;
cin >> s;
int l = 0;
int r = n - 1;
forn(i, n - 1)//看清楚这里是n-1不是n,所以并没有遍历整个LRRLL数组,只遍历到n-2位置
if (s[i] == 'L')
l++;
else
r--;
assert(lr);
vector
b[n - 1] = a[l] % m;//最后的结果l
for (int i = n - 2; i >= 0; i--) {//
if (s[i] == 'L')
b[i] = (b[i + 1] * a[--l]) % m;//b[i+1]==a[l]%m,第i个移除元素模m的结果
else
b[i] = (b[i + 1] * a[++r]) % m;
}
assert(l == 0);
assert(r == n - 1);
forn(i, n)
cout << b[i] << " ";
cout << endl;
}
}