結果

問題 No.672 最長AB列
ユーザー cympfhcympfh
提出日時 2018-06-13 16:50:48
言語 Rust
(1.77.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,873 bytes
コンパイル時間 1,243 ms
コンパイル使用メモリ 163,864 KB
実行使用メモリ 5,784 KB
最終ジャッジ日時 2023-09-13 03:55:06
合計ジャッジ時間 2,645 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 15 ms
5,784 KB
testcase_10 AC 13 ms
4,380 KB
testcase_11 AC 12 ms
4,380 KB
testcase_12 AC 8 ms
4,380 KB
testcase_13 AC 9 ms
4,384 KB
testcase_14 AC 8 ms
4,376 KB
testcase_15 AC 8 ms
4,376 KB
testcase_16 AC 8 ms
4,380 KB
testcase_17 AC 12 ms
4,376 KB
testcase_18 AC 7 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: constant `M` is never used
  --> Main.rs:26:7
   |
26 | const M: usize = 1_000_000_007;
   |       ^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

#![allow(unused_imports)]
use std::io::{ self, Write };
use std::str::FromStr;
use std::cmp::{ min, max };
use std::collections::{ BinaryHeap, VecDeque, HashMap };

#[allow(unused_macros)]
macro_rules! trace {
    ($var:expr) => {
        let _ = writeln!(&mut std::io::stderr(), ">>> {} = {:?}", stringify!($var), $var);
    };
    ($($args:expr),*) => { trace!(($($args),*)) }
}

#[allow(unused_macros)]
macro_rules! put {
    ($var:expr) => {
        let _ = writeln!(&mut std::io::stdout(), "{}", $var);
    };
    ($var:expr, $($args:expr),*) => {
        let _ = write!(&mut std::io::stdout(), "{} ", $var);
        put!($($args),*);
    };
}

const M: usize = 1_000_000_007;

fn main() {
    let mut sc = Scanner::new();
    let mut ans = 0;
    let mut memo = HashMap::new();
    let mut ac = 0;
    let mut idx = 1;
    memo.insert(0, 0);
    for c in sc.cin::<String>().chars() {
        if c == 'A' {
            ac += 1;
        } else {
            ac -= 1;
        }
        if memo.contains_key(&ac) {
            ans = max(ans, idx - memo[&ac]);
        } else {
            memo.insert(ac, idx);
        }
        idx += 1;
    }
    put!(ans);
}

#[allow(dead_code)]
struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
#[allow(dead_code)]
impl Scanner {
    fn new() -> Scanner { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn reserve(&mut self) {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
    }
    fn cin<T: FromStr>(&mut self) -> T {
        self.reserve();
        match self.buffer.pop_front().unwrap().parse::<T>() {
            Ok(a) => a,
            Err(_) => panic!("parse err")
        }
    }
}
0