https://atcoder.jp/contests/abc365/tasks/abc365_d
単なるDPですね。
// AtCoder Janken 3 #![allow(non_snake_case)] //////////////////// library //////////////////// fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_line(&mut line).ok(); line.trim().parse().ok().unwrap() } //////////////////// process //////////////////// use std::cmp::max; fn read_input() -> String { let _N: usize = read(); let S: String = read(); S } type DP = [i32; 3]; fn update_dp(hand: char, dp: DP) -> DP { let mut new_dp: DP = [0, 0, 0]; let i: usize = match hand { 'R' => 0, 'P' => 1, _ => 2 }; for k in 0..3 { // 前回の自分の手 for l in 0..3 { // 今回の自分の手 if k == l { continue } else if i == (l + 1) % 3 { // 相手の勝ち continue } else if (i + 1) % 3 == l { // 自分の勝ち new_dp[l] = max(new_dp[l], dp[k] + 1) } else { new_dp[l] = max(new_dp[l], dp[k]) } } } new_dp } fn F(S: String) -> i32 { let mut dp: DP = [0, 0, 0]; for hand in S.chars() { dp = update_dp(hand, dp) } *dp.iter().max().unwrap() } fn main() { let S = read_input(); println!("{}", F(S)) }