結果

問題 No.104 国道
ユーザー yukyu
提出日時 2021-03-08 00:38:10
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,117 bytes
コンパイル時間 15,736 ms
コンパイル使用メモリ 393,880 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-09 10:51:41
合計ジャッジ時間 14,677 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;
use std::str::FromStr;

fn main() {
    exec(read());
}

fn exec(i: String) {
    if i.is_empty() {
        return println!("{}", 1);
    }
    let char_list: Vec<char> = i.chars().collect();
    let length = char_list.len();
    let leftmost = multiple(length);
    let index = get_index(char_list);
    println!("{}", leftmost + index - 1);
}

fn multiple(i: usize) -> i64 {
    rec_power2(i, 1)
}

fn rec_power2(i: usize, acc: i64) -> i64 {
    if i == 0 {
        return acc;
    } else {
        rec_power2(i - 1, acc * 2)
    }
}

fn get_index(mut l: Vec<char>) -> i64 {
    let head = l.remove(0);
    if head == 'L' {
        rec_get_index(l, 1)
    } else {
        rec_get_index(l, 2)
    }
}

fn rec_get_index(mut l: Vec<char>, acc: i64) -> i64 {
    if l.is_empty() {
        return acc;
    }

    let head = l.remove(0);
    if head == 'L' {
        return rec_get_index(l, (acc * 2) - 1);
    } else {
        return rec_get_index(l, acc * 2);
    }
}

fn read<T: FromStr>() -> T {
    let mut s = String::new();
    stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}
0