結果

問題 No.962 LCPs
ユーザー tonyu0tonyu0
提出日時 2019-12-26 19:54:39
言語 Rust
(1.77.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 1,629 bytes
コンパイル時間 1,838 ms
コンパイル使用メモリ 163,824 KB
実行使用メモリ 13,272 KB
最終ジャッジ日時 2024-04-15 06:58:54
合計ジャッジ時間 4,186 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 0 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 18 ms
13,272 KB
testcase_18 AC 11 ms
7,552 KB
testcase_19 AC 11 ms
7,680 KB
testcase_20 AC 8 ms
6,944 KB
testcase_21 AC 8 ms
6,944 KB
testcase_22 AC 6 ms
6,940 KB
testcase_23 AC 7 ms
6,944 KB
testcase_24 AC 7 ms
6,940 KB
testcase_25 AC 7 ms
6,940 KB
testcase_26 AC 6 ms
6,944 KB
testcase_27 AC 8 ms
6,944 KB
testcase_28 AC 6 ms
6,940 KB
testcase_29 AC 5 ms
6,940 KB
testcase_30 AC 6 ms
6,940 KB
testcase_31 AC 8 ms
6,940 KB
testcase_32 AC 6 ms
6,940 KB
testcase_33 AC 10 ms
7,424 KB
testcase_34 AC 6 ms
6,944 KB
testcase_35 AC 6 ms
6,940 KB
testcase_36 AC 7 ms
6,944 KB
testcase_37 AC 5 ms
6,940 KB
testcase_38 AC 6 ms
6,940 KB
testcase_39 AC 6 ms
6,944 KB
testcase_40 AC 5 ms
6,940 KB
testcase_41 AC 6 ms
6,944 KB
testcase_42 AC 5 ms
6,940 KB
testcase_43 AC 5 ms
6,944 KB
testcase_44 AC 6 ms
6,944 KB
testcase_45 AC 5 ms
6,940 KB
testcase_46 AC 5 ms
6,944 KB
testcase_47 AC 2 ms
6,940 KB
testcase_48 AC 2 ms
6,944 KB
testcase_49 AC 2 ms
6,940 KB
testcase_50 AC 3 ms
6,940 KB
testcase_51 AC 2 ms
6,940 KB
testcase_52 AC 1 ms
6,944 KB
testcase_53 AC 2 ms
6,940 KB
testcase_54 AC 2 ms
6,944 KB
testcase_55 AC 1 ms
6,940 KB
testcase_56 AC 2 ms
6,944 KB
testcase_57 AC 3 ms
6,940 KB
testcase_58 AC 2 ms
6,940 KB
testcase_59 AC 2 ms
6,940 KB
testcase_60 AC 3 ms
6,944 KB
testcase_61 AC 2 ms
6,944 KB
testcase_62 AC 2 ms
6,944 KB
testcase_63 AC 12 ms
9,268 KB
testcase_64 AC 2 ms
6,940 KB
testcase_65 AC 14 ms
9,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;

fn main() {
  let mut s: String = String::new();
  std::io::stdin().read_to_string(&mut s).ok();
  let mut itr = s.trim().split_whitespace();
  let n: usize = itr.next().unwrap().parse().unwrap();
  let s: Vec<Vec<char>> = (0..n)
    .map(|_| itr.next().unwrap().chars().collect())
    .collect();

  let mut ans: u64 = 0;
  let mut stack: Vec<(u64, u64)> = Vec::new();
  for i in 0..n {
    ans += s[i].len() as u64;
    if i == n - 1 {
      while let Some((cnt, pos)) = stack.pop() {
        let l = i as u64 - pos + 1;
        if let Some((c, p)) = stack.pop() {
          ans += (cnt - c) * (l * (l + 1) * (l + 2) / 6 - l);
          stack.push((c, p));
        } else {
          ans += cnt * (l * (l + 1) * (l + 2) / 6 - l);
        }
      }
      break;
    }
    let mut m: u64 = 0;
    let k = std::cmp::min(s[i].len(), s[i + 1].len());
    for j in 0..k {
      if s[i][j] != s[i + 1][j] {
        break;
      }
      m += 1;
    }
    // 今よりマッチ数が小さくなるまでpop
    let mut next: u64 = i as u64;
    while let Some((cnt, pos)) = stack.pop() {
      if cnt == m {
        next = pos;
        break;
      } else if cnt < m {
        stack.push((cnt, pos));
        break;
      }

      let l = i as u64 - pos + 1;
      if let Some((c, p)) = stack.pop() {
        ans += (cnt - std::cmp::max(m, c)) * (l * (l + 1) * (l + 2) / 6 - l);
        // 自分と同じ一番前を取得
        stack.push((c, p));
      } else {
        ans += (cnt - m) * (l * (l + 1) * (l + 2) / 6 - l);
      }
      next = pos;
    }
    stack.push((m, next));
  }
  println!("{}", ans);
}
0