Skip to content

Instantly share code, notes, and snippets.

@tokugh
Last active July 2, 2021 04:02
Show Gist options
  • Save tokugh/3028523950cf61839b13506f31753f6c to your computer and use it in GitHub Desktop.
Save tokugh/3028523950cf61839b13506f31753f6c to your computer and use it in GitHub Desktop.
use proconio::{input, fastout, marker::Chars} ;
#[fastout]
fn main() {
input! {
n: usize,
s: Chars,
t: Chars,
};
let s: Vec<_> = s.into_iter().map(|x| match x {'R'=>0,'G'=>1,'B'=>2,_=>3}).collect();
let t: Vec<_> = t.into_iter().map(|x| match x {'R'=>0,'G'=>1,'B'=>2,_=>3}).collect();
let r#mod = 1_000_000_007;
let r#b = 5;
let mut pm = [0; 1_000_000];
pm[0] = 1;
for i in 1..1_000_000 { pm[i] = pm[i-1] * r#b % r#mod; }
let pm = pm;
let mut ans = 0;
for m in 0..3 {
let cl = |x| ((6 - x as i64 - m) % 3);
let mut hasher_s = 0;
let mut hasher_t = 0;
for i in 0..n {
hasher_s = (r#b * hasher_s + s[i]) % r#mod;
hasher_t = (pm[i] * cl( t[n-1-i] ) + hasher_t) % r#mod;
if hasher_s == hasher_t { ans += 1; }
}
let mut hasher_s = 0;
let mut hasher_t = 0;
for i in 0..n-1 {
hasher_s = (pm[i] * s[n-1-i] + hasher_s) % r#mod;
hasher_t = (r#b * hasher_t + cl( t[i] )) % r#mod;
if hasher_s == hasher_t { ans += 1; }
}
}
println!("{}", ans);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment