結果

問題 No.672 最長AB列
ユーザー lzy9lzy9
提出日時 2018-04-13 23:52:33
言語 Rust
(1.77.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 1,049 bytes
コンパイル時間 8,718 ms
コンパイル使用メモリ 154,444 KB
実行使用メモリ 4,476 KB
最終ジャッジ日時 2023-09-10 04:14:09
合計ジャッジ時間 5,300 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 6 ms
4,384 KB
testcase_10 AC 5 ms
4,384 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 6 ms
4,384 KB
testcase_13 AC 6 ms
4,384 KB
testcase_14 AC 6 ms
4,384 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 6 ms
4,380 KB
testcase_17 AC 5 ms
4,476 KB
testcase_18 AC 5 ms
4,384 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `min`
 --> Main.rs:3:16
  |
3 | use std::cmp::{min, max};
  |                ^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `std::mem::swap`
 --> Main.rs:4:5
  |
4 | use std::mem::swap;
  |     ^^^^^^^^^^^^^^

warning: unused import: `std::collections::HashMap`
 --> Main.rs:5:5
  |
5 | use std::collections::HashMap;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^

warning: static `DX` is never used
  --> Main.rs:19:8
   |
19 | static DX: &'static [i32] = &[0, 0, 1, -1];
   |        ^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: static `DY` is never used
  --> Main.rs:20:8
   |
20 | static DY: &'static [i32] = &[1, -1, 0, 0];
   |        ^^

warning: 5 warnings emitted

ソースコード

diff #

use std::io::*;
use std::str::FromStr;
use std::cmp::{min, max};
use std::mem::swap;
use std::collections::HashMap;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin_lock = stdin.lock();
    let s = stdin_lock
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<String>();
    s.parse::<T>().ok().unwrap()
}

static DX: &'static [i32] = &[0, 0, 1, -1];
static DY: &'static [i32] = &[1, -1, 0, 0];

fn main() {
    let s: String = read();

    let z = 200010;

    let mut t = z;

    let mut idx = vec![0; z * 2];
    let mut visited = vec![false; z * 2];

    visited[z] = true;

    let mut ans = 0;

    for (i, c) in s.chars().enumerate() {
        if c == 'A' {
            t += 1;
        } else {
            t -= 1;
        }

        if visited[t] {
            ans = max(ans, i + 1 - idx[t]);
        } else {
            idx[t] = i + 1;
            visited[t] = true;
        }
    }

    println!("{}", ans);
}
0