QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#662724 | #4567. Admissible Map | IllusionaryDominance | WA | 1ms | 4168kb | C++20 | 4.1kb | 2024-10-21 09:32:01 | 2024-10-21 09:32:01 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 20000 + 5;
const int P = 1000000000 + 9;
const int Base = 7;
char str[MAX_N];
int N, f[MAX_N], pw[MAX_N], a[MAX_N], len[MAX_N], ru[MAX_N], rl[MAX_N], tot;
vector <int> qry1[MAX_N], qry2[MAX_N];
struct Hash{
int ha[MAX_N];
void init(int *a) {
for (int i = N; i > 0; i --) {
ha[i] = (7ll * ha[i + 1] + a[i]) % P;
}
// for (int i = 1; i <= N; i ++) {
// ha[i] = (7ll * ha[i - 1] + a[i]) % P;
// }
}
int query(int l, int r) {
return (ha[l] - 1ll * pw[r - l + 1] * ha[r + 1] + 1ll * P * P) % P;
// return (ha[r] - 1ll * pw[r - l + 1] * ha[l - 1] + 1ll * P * P) % P;
}
}hal, har, hau, had, pat;
int power(int a, int n) {
int ans = 1;
while (n) {
if (n & 1) ans = 1ll * ans * a % P;
a = 1ll * a * a % P; n >>= 1;
}
return ans;
}
void init_hash() {
pw[0] = 1;
const int inv7 = power(7, P - 2);
for (int i = 1; i <= N; i ++) {
pw[i] = 7ll * pw[i - 1] % P;
a[i] = str[i] == 'L' ? inv7 : 0;
}
hal.init(a);
for (int i = 1; i <= N; i ++) {
a[i] = str[i] == 'R' ? 7 : 0;
}
har.init(a);
for (int i = 1; i <= N; i ++) {
a[i] = str[i] == 'U';
}
hau.init(a);
for (int i = 1; i <= N; i ++) {
a[i] = str[i] == 'D';
}
had.init(a);
for (int i = 1; i <= N; i ++) {
a[i] = 1;
}
pat.init(a);
}
inline int add(int a, int b) {return a + b < P ? a + b : a + b - P;}
bool check_top(int l, int r) {
int m = r - l + 1;
return str[l] != 'L' && str[r] != 'R' && !hau.query(l, r) &&
add(add(har.query(l, r), hal.query(l, r)), hau.query(r + 1, r + m)) == pat.query(l, r);
}
bool check_mid(int l, int r) {
int m = r - l + 1;
return str[l] != 'L' && str[r] != 'R' &&
add(add(had.query(l - m, r - 1), hau.query(l + 1, r + m)), add(hal.query(l, r), har.query(l, r))) == pat.query(l, r);
}
bool check_bottom(int l, int r) {
int m = r - l + 1;
return str[l] != 'L' && str[r] != 'R' && !had.query(l, r) &&
add(had.query(l - m, r - 1), add(hal.query(l, r), har.query(l, r))) == pat.query(l, r);
}
int main() {
scanf("%s", str + 1);
N = strlen(str + 1);
int ans = 0;
for (int i = N; i > 0; i --) {
if (str[i] == 'R' && str[i + 1] == 'L') {
f[i] = f[i + 2] + 1;
}
ru[i] = str[i] == 'U' ? i : ru[i + 1];
rl[i] = str[i] == 'L' ? rl[i + 1] + 1 : 0;
ans += f[i];
int p = i + (f[i] << 1);
if (str[p] == 'R') {
int q = ru[p];
if (q) len[i] = q - p;
}else if (str[p] == 'D') {
int q = p + rl[p + 1];
assert(q <= N);
int r = ru[q];
if (r) {
assert(r > q);
len[i] = r - q;
}
}
if (len[i]) {
if ((~ len[i] & 1) || ((f[i] << 1) < len[i])) qry1[len[i]].push_back(i);
if ((~ len[i] & 1) && ((f[i] << 1) >= len[i])) ans -= (f[i] << 1) / len[i] - 1;
}
}
init_hash();
for (int i = 1; i <= N / 2; i ++) {
if (qry1[i].empty()) continue;
map <int, int> idx;
int j = tot;
for (auto pos : qry1[i]) {
if (idx.find(pos % i) == idx.end()) idx[pos % i] = ++ tot;
if (check_top(pos, pos + i - 1)) {
qry2[idx[pos % i]].push_back(pos);
}
}
for (j ++; j <= tot; j ++) {
if (qry2[j].empty()) continue;
sort(qry2[j].begin(), qry2[j].end());
for (int k = qry2[j][0], l = 0, r = 0, lst = k; k + i - 1 <= N; k += i) {
while (l < r && qry2[j][l] < lst) l ++;
if (check_bottom(k, k + i - 1)) ans += r - l;
while (r < (int)qry2[j].size() && qry2[j][r] == k) r ++;
if (!check_mid(k, k + i - 1)) lst = k;
}
}
}
printf("%d\n", ans);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3876kb
input:
RDUL
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 1ms
memory: 4168kb
input:
RDRU
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 1ms
memory: 4088kb
input:
RLRLRL
output:
6
result:
ok 1 number(s): "6"
Test #4:
score: 0
Accepted
time: 1ms
memory: 4152kb
input:
D
output:
0
result:
ok 1 number(s): "0"
Test #5:
score: 0
Accepted
time: 1ms
memory: 4160kb
input:
DL
output:
0
result:
ok 1 number(s): "0"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3864kb
input:
RL
output:
1
result:
ok 1 number(s): "1"
Test #7:
score: 0
Accepted
time: 1ms
memory: 3948kb
input:
DU
output:
1
result:
ok 1 number(s): "1"
Test #8:
score: 0
Accepted
time: 1ms
memory: 3828kb
input:
RDULU
output:
2
result:
ok 1 number(s): "2"
Test #9:
score: 0
Accepted
time: 1ms
memory: 3936kb
input:
RDDUULRDDD
output:
2
result:
ok 1 number(s): "2"
Test #10:
score: 0
Accepted
time: 1ms
memory: 3900kb
input:
RLRLRLRLRL
output:
15
result:
ok 1 number(s): "15"
Test #11:
score: 0
Accepted
time: 1ms
memory: 3880kb
input:
DUDUDUDUDU
output:
15
result:
ok 1 number(s): "15"
Test #12:
score: 0
Accepted
time: 1ms
memory: 3912kb
input:
RDRDULULRR
output:
3
result:
ok 1 number(s): "3"
Test #13:
score: 0
Accepted
time: 1ms
memory: 3828kb
input:
UUURDRDRDLRULLRLULUDRURDRULLRU
output:
2
result:
ok 1 number(s): "2"
Test #14:
score: 0
Accepted
time: 1ms
memory: 4088kb
input:
RLRLRLRLRLRLRLRLRLRLRLRLRLRLRL
output:
120
result:
ok 1 number(s): "120"
Test #15:
score: 0
Accepted
time: 1ms
memory: 4168kb
input:
DUDUDUDUDUDUDUDUDUDUDUDUDUDUDU
output:
120
result:
ok 1 number(s): "120"
Test #16:
score: 0
Accepted
time: 1ms
memory: 3884kb
input:
RDRDRDRDRDRDRDULULULULULULULDL
output:
8
result:
ok 1 number(s): "8"
Test #17:
score: -100
Wrong Answer
time: 1ms
memory: 4140kb
input:
DLURRURRLDDUUDRDURDDLRLULLULDUULDDRDUURLLDDLRRLUDURLLRRRLLRLUDULDULUUDLLDLRLULDDURDDDLLUURRDRRDDRULU
output:
17
result:
wrong answer 1st numbers differ - expected: '18', found: '17'