QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#190556 | #3293. 优秀的拆分 | Qingyu | 100 ✓ | 124ms | 15676kb | Rust | 6.7kb | 2023-09-29 04:10:03 | 2023-09-29 04:10:03 |
Judging History
answer
#![allow(non_upper_case_globals, unused_imports)]
#![allow(clippy::indexing_slicing)]
use std::cmp::{max, min};
use std::fmt::Debug;
use std::io::{self, BufRead, Write};
struct A {
len: usize,
s: [u8; sz],
sa: [usize; sz],
rk: [usize; sz << 1],
height: [usize; sz],
f: [[usize; log_num]; sz],
}
static mut sa: A = A {
len: 0,
s: [0; sz],
sa: [0; sz],
rk: [0; sz << 1],
height: [0; sz],
f: [[0; log_num]; sz],
};
static mut pa: A = A {
len: 0,
s: [0; sz],
sa: [0; sz],
rk: [0; sz << 1],
height: [0; sz],
f: [[0; log_num]; sz],
};
static mut oldrk: [usize; sz << 1] = [0; sz << 1];
static mut id: [usize; sz] = [0; sz];
static mut key: [usize; sz] = [0; sz];
static mut cnt: [usize; sz] = [0; sz];
static mut log: [usize; sz] = [0; sz];
static mut a: [i32; sz] = [0; sz];
static mut b: [i32; sz] = [0; sz];
static mut m: usize = 128;
const sz: usize = 30010;
const log_num: usize = 21;
fn main() {
let stdin = io::stdin();
let stdout = io::stdout();
unsafe {
solve(stdin.lock(), stdout.lock());
}
}
unsafe fn solve<R, W>(mut input: R, mut output: W)
where
R: BufRead,
W: Write,
{
let mut buf = String::new();
input.read_line(&mut buf).unwrap();
let arg: Vec<usize> = buf
.split_whitespace()
.map(|s| s.parse::<usize>().unwrap())
.collect();
log[2] = 1;
for i in 3..sz {
log[i] = log[i / 2] + 1;
}
for _ in 0..arg[0] {
let mut buf = String::new();
input.read_line(&mut buf).unwrap();
if buf.ends_with('\n') {
buf.pop();
}
if buf.ends_with('\r') {
buf.pop();
}
let str = buf.into_bytes();
let n = str.len();
sa.len = n;
pa.len = n;
for i in 0..n {
sa.s[i] = str[i];
pa.s[i] = str[n - i - 1];
}
sa.init();
pa.init();
for len in 1..n / 2 + 1 {
for pt1 in (len - 1..n - len).step_by(len) {
let pt2 = pt1 + len;
if pt2 >= n {
break;
}
let lcp = min(sa.find(pt1, pt2), len);
let lcs = min(pa.find(n - pt2, n - pt1), len - 1);
if lcp + lcs >= len {
let lap = lcp + lcs - len + 1;
b[pt1 - lcs] += 1;
b[pt1 - lcs + lap] -= 1;
a[pt2 + lcp - lap] += 1;
a[pt2 + lcp] -= 1;
}
}
}
for i in 1..n + 1 {
a[i] += a[i - 1];
b[i] += b[i - 1];
}
let mut result: u64 = 0;
for i in 0..n - 1 {
result += a[i] as u64 * b[i + 1] as u64;
}
writeln!(output, "{}", result).unwrap();
a.fill(0);
b.fill(0);
sa.clear();
pa.clear();
}
}
impl A {
unsafe fn init(&mut self) {
let n = self.len;
for i in 0..n {
self.rk[i] = self.s[i] as usize;
cnt[self.rk[i]] += 1;
}
for i in 1..m {
cnt[i] += cnt[i - 1];
}
for i in (0..n).rev() {
cnt[self.rk[i]] -= 1;
self.sa[cnt[self.rk[i]]] = i;
}
let mut w = 1;
let mut p;
while w <= n {
p = 0;
for i in (n - w..n).rev() {
id[p] = i;
p += 1;
}
for i in self.sa.iter().take(n) {
if *i >= w {
id[p] = *i - w;
p += 1;
}
}
cnt.fill(0);
for i in 0..n {
key[i] = self.rk[id[i]];
cnt[key[i]] += 1;
}
for i in 1..m {
cnt[i] += cnt[i - 1];
}
for i in (0..n).rev() {
cnt[key[i]] -= 1;
self.sa[cnt[key[i]]] = id[i];
}
oldrk.copy_from_slice(&self.rk[..]);
self.rk[self.sa[0]] = 0;
p = 0;
for i in 1..n {
if !cmp(self.sa[i], self.sa[i - 1], w) {
p += 1;
}
self.rk[self.sa[i]] = p;
}
if p + 1 == n {
for i in 0..n {
self.sa[self.rk[i]] = i;
}
break;
}
w <<= 1;
m = p + 1;
}
for i in 0..n {
self.rk[self.sa[i]] = i;
}
p = 0;
for i in 0..n {
if self.rk[i] == 0 {
continue;
}
p = p.saturating_sub(1);
while self.s[i + p] == self.s[self.sa[self.rk[i] - 1] + p] {
p += 1;
}
self.height[self.rk[i]] = p;
}
m = 128;
cnt.fill(0);
oldrk.fill(0);
id.fill(0);
key.fill(0);
for i in 0..n {
self.f[i][0] = self.height[i];
}
for j in 1..log_num {
let mut i = 0;
while i + (1 << j) <= n {
self.f[i][j] = min(self.f[i][j - 1], self.f[i + (1 << (j - 1))][j - 1]);
i += 1;
}
}
}
unsafe fn find(&self, x: usize, y: usize) -> usize {
let i = min(self.rk[x], self.rk[y]) + 1;
let j = max(self.rk[x], self.rk[y]) + 1;
let step = log[j - i];
min(self.f[i][step], self.f[j - (1 << step)][step])
}
fn clear(&mut self) {
self.f.fill([0; log_num]);
self.sa.fill(0);
self.rk.fill(0);
self.height.fill(0);
self.s.fill(0);
}
}
unsafe fn cmp(x: usize, y: usize, w: usize) -> bool {
oldrk[x] == oldrk[y] && oldrk[x + w] == oldrk[y + w]
}
impl Debug for A {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "len: {}", self.len).unwrap();
writeln!(f, "s: ").unwrap();
write!(f, "[ ").unwrap();
for i in self.s.iter().take(self.len) {
write!(f, "{i} ").unwrap();
}
writeln!(f, "]").unwrap();
writeln!(f, "sa: ").unwrap();
write!(f, "[ ").unwrap();
for i in self.sa.iter().take(self.len) {
write!(f, "{i} ").unwrap();
}
writeln!(f, "]").unwrap();
writeln!(f, "rk: ").unwrap();
write!(f, "[ ").unwrap();
for i in self.rk.iter().take(self.len) {
write!(f, "{i} ").unwrap();
}
writeln!(f, "]")
}
}
詳細信息
Test #1:
score: 5
Accepted
time: 9ms
memory: 15520kb
input:
10 wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk...
output:
462056 140600 875978 575960 811035 299536 173880 414090 227128 924490
result:
ok 10 numbers
Test #2:
score: 5
Accepted
time: 10ms
memory: 15384kb
input:
10 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq...
output:
369564 513590 285760 308945 408312 190568 180441 328350 437635 1091574
result:
ok 10 numbers
Test #3:
score: 5
Accepted
time: 13ms
memory: 15408kb
input:
10 kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk...
output:
92601930 126763350 233408728 111659395 147774025 41791750 62056280 198982282 172593608 298613460
result:
ok 10 numbers
Test #4:
score: 5
Accepted
time: 17ms
memory: 15480kb
input:
10 nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn...
output:
241383298 63369600 53220335 75846485 53073182 47276061 287600152 128609208 205431400 71461299
result:
ok 10 numbers
Test #5:
score: 5
Accepted
time: 7ms
memory: 15476kb
input:
10 nnnnnnnnnn hzhzhzhzhz zezezezeze ndndndndnd zvzvzvzvzv bbbbbbbbbb sgsgsgsgsg fgfgfgfgfg hxhxhxhxhx fafafafafa
output:
30 3 3 3 3 30 3 3 3 3
result:
ok 10 numbers
Test #6:
score: 5
Accepted
time: 7ms
memory: 15480kb
input:
10 xxxxxxxxxx wtwtwtwtwt sososososo xgxgxgxgxg lblblblblb rororororo qfqfqfqfqf qjqjqjqjqj upupupupup nynynynyny
output:
30 3 3 3 3 3 3 3 3 3
result:
ok 10 numbers
Test #7:
score: 5
Accepted
time: 10ms
memory: 15476kb
input:
10 jjjjjjjjjjjjjjjjjjjj tgtgtgtgtg mcomcomcomco wthwthwthwth aaitaaitaaitaait pvaompvaompvaompvaom ahahahahah jwxmjwxmjwxmjwxm pdfpdfpdfpdf oiyfoiyfoiyfoiyf
output:
285 3 1 1 5 1 3 1 1 1
result:
ok 10 numbers
Test #8:
score: 5
Accepted
time: 11ms
memory: 15624kb
input:
10 iiiiiiiiiiiiiiiii scscscscscscsc bsobsobsobsobso rppyrppyrppyrppy xhnxhnxhnxhn jfbjfbjfbjfb tqtqtqtqtq cdocdocdocdocdo imgimgimgimg zxzxzxzxzx
output:
168 13 4 5 1 1 3 4 1 3
result:
ok 10 numbers
Test #9:
score: 5
Accepted
time: 10ms
memory: 15516kb
input:
10 sssssssssssssssssssss wawawawawawawawa xlexlexlexlexlexlexle cwqfcwqfcwqfcwqfcwqfcwqfcwqf dappidappidappidappidappi biwtbbiwtbbiwtbbiwtb ipdbipdbipdbipdb oyqjoyqjoyqjoyqj miqozmiqozmiqozmiqoz ofqgofqgofqgofqg
output:
330 22 18 23 14 3 1 1 1 1
result:
ok 10 numbers
Test #10:
score: 5
Accepted
time: 8ms
memory: 15624kb
input:
10 zzzzzzzzzzzzzzzzzzzzzzzzzzz icicicicicicicicicicic saasaasaasaasaasaasaa znunznunznunznunznun ttfhhttfhhttfhhttfhhttfhh fqxqblfqxqblfqxqblfqxqblfqxqbl xxpruxxpruxxpruxxpru mpheqsmpheqsmpheqsmpheqs ptvbemqptvbemqptvbemqptvbemq nxykqknxykqknxykqknxykqk
output:
728 70 36 5 26 7 5 1 1 1
result:
ok 10 numbers
Test #11:
score: 5
Accepted
time: 8ms
memory: 15420kb
input:
10 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj ibibibibibibibibibibibibibibibibibibibibibibibib xinxinxinxinxinxinxinxinxinxinxin ivlwivlwivlwivlwivlwivlw cxrvucxrvucxrvucxrvucxrvucxrvucxrvucxrvucxrvu jzqfmgjzqfmgjzqfmgjzqfmgjzqfmg nqctlysnqctlysnqctlysnqctlysnqctlysnqctlysnqctlys kuizuntnkuizuntnkuizuntnkuizu...
output:
1240 946 100 11 76 7 38 9 18 1
result:
ok 10 numbers
Test #12:
score: 5
Accepted
time: 7ms
memory: 15388kb
input:
10 vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv dvdvdvdvdvdvdvdvdvdvdvdvdv jhyjhyjhyjhyjhyjhyjhyjhyjhy dkbjdkbjdkbjdkbjdkbjdkbjdkbjdkbj ezlbtezlbtezlbtezlbtezlbtezlbtezlbtezlbt efwduoefwduoefwduoefwduoefwduoefwduoefwduo eqgdlgxeqgdlgxeqgdlgxeqgdlgxeqgdlgxeqgdlgx mwuoqleamwuoqleamwuoqleamwuoqlea gwcbhodfpgwcb...
output:
1632 125 48 38 46 33 17 1 1 5
result:
ok 10 numbers
Test #13:
score: 5
Accepted
time: 8ms
memory: 15676kb
input:
10 vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv xwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxw rslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrsl iimeiimeiimeiimeiimeiimeiimeiimeiimeiimeiimeiime...
output:
25585 6391 1386 1014 876 567 62 118 33 86
result:
ok 10 numbers
Test #14:
score: 5
Accepted
time: 8ms
memory: 15484kb
input:
10 rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr ozozozozozozozozozozozozozozozozozozozozozozozozozozozozozozozoz ysvysvysvysvysvysvysvysvysvysvysvysvysvysvysvysvysvysv ipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbd x...
output:
19019 2360 540 1826 110 7 511 118 132 53
result:
ok 10 numbers
Test #15:
score: 5
Accepted
time: 8ms
memory: 15400kb
input:
10 ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss vzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvz yzkyzkyzkyzkyzk...
output:
155155 18445 7600 13475 3328 72 114 37 17 6
result:
ok 10 numbers
Test #16:
score: 5
Accepted
time: 13ms
memory: 15404kb
input:
10 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii bxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbx...
output:
281295 48503 26100 13475 20516 80 72 59 21 26
result:
ok 10 numbers
Test #17:
score: 5
Accepted
time: 9ms
memory: 15468kb
input:
10 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii...
output:
1391040 981179 345415 50677 52576 227 28 413 144 50
result:
ok 10 numbers
Test #18:
score: 5
Accepted
time: 7ms
memory: 15408kb
input:
10 fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff...
output:
38263590 33623065 4070400 510708 624588 2102 1074 299 408 1538
result:
ok 10 numbers
Test #19:
score: 5
Accepted
time: 12ms
memory: 15424kb
input:
10 qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq...
output:
245030555 46877978 4884100 14318590 12858978 5063 5381 7598 4729 5228
result:
ok 10 numbers
Test #20:
score: 5
Accepted
time: 124ms
memory: 15568kb
input:
10 yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy...
output:
563349754956 161455324997 76621205738 70150901846 40842068960 6056659 2820346 3357795 2628223 10884
result:
ok 10 numbers
Extra Test:
score: 0
Extra Test Passed