結果

問題 No.599 回文かい
ユーザー hatoohatoo
提出日時 2017-11-25 03:23:09
言語 Rust
(1.77.0)
結果
AC  
実行時間 133 ms / 4,000 ms
コード長 3,681 bytes
コンパイル時間 1,524 ms
コンパイル使用メモリ 143,068 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 05:55:49
合計ジャッジ時間 2,069 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 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,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 62 ms
4,380 KB
testcase_15 AC 4 ms
4,380 KB
testcase_16 AC 117 ms
4,376 KB
testcase_17 AC 133 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
evil_0.txt AC 27 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet, BinaryHeap, VecDeque, BTreeSet, BTreeMap};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_imports)]
use std::io::stdin;

mod util {
    use std::io::stdin;
    use std::str::FromStr;
    use std::fmt::Debug;

    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
}

#[allow(unused_macros)]
macro_rules! get {
    ($t:ty) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t:ty),*) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            (
                $(iter.next().unwrap().parse::<$t>().unwrap(),)*
            )
        }
    };
    ($t:ty; $n:expr) => {
        (0..$n).map(|_|
                    get!($t)
                   ).collect::<Vec<_>>()
    };
    ($($t:ty),*; $n:expr) => {
        (0..$n).map(|_|
                    get!($($t),*)
                   ).collect::<Vec<_>>()
    };
    ($t:ty ;;) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|t| t.parse::<$t>().unwrap())
                .collect::<Vec<_>>()
        }
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

struct RollingHash {
    hash: Vec<u64>,
    pow: Vec<u64>,
}

impl RollingHash {
    const M: u64 = 1000000007;
    const B: u64 = 1009;

    fn new(s: &[u64]) -> RollingHash {
        let mut hash = Vec::with_capacity(s.len() + 1);
        let mut pow = Vec::with_capacity(s.len() + 1);

        hash.push(0);
        pow.push(1);

        for (i, &x) in s.iter().enumerate() {
            let h = hash[i];
            let p = pow[i];
            hash.push((h + x) * Self::B % Self::M);
            pow.push(p * Self::B % Self::M);
        }

        RollingHash {
            hash: hash,
            pow: pow,
        }
    }

    fn get(&self, l: usize, r: usize) -> u64 {
        (self.hash[r] + Self::M - self.hash[l] * self.pow[r - l] % Self::M) % Self::M
    }

    fn len(&self) -> usize {
        self.hash.len() - 1
    }
}

const M: usize = 1000000007;

fn rec(rollig_hash: &RollingHash, i: usize, memo: &mut [Option<usize>]) -> usize {
    if let Some(res) = memo[i] {
        return res;
    }

    let mut sum = 1;

    for k in 1.. {
        if i + k > rollig_hash.len() - i - k {
            break;
        }
        if rollig_hash.get(i, i + k) ==
            rollig_hash.get(rollig_hash.len() - i - k, rollig_hash.len() - i)
        {
            sum += rec(rollig_hash, i + k, memo);
            sum %= M;
        }
    }

    memo[i] = Some(sum);

    sum
}

fn main() {
    let s: Vec<u64> = util::line().chars().map(|c| c as u64).collect();
    let rolling_hash = RollingHash::new(&s);

    let mut memo = vec![None; s.len()];
    println!("{}", rec(&rolling_hash, 0, &mut memo));
}
0