Submission #19682863


Source Code Expand

Copy
use std::fmt;
use std::io::stdin;
use std::io::BufRead;
use std::str;
use std::str::FromStr;

fn main() {
	let stdin = stdin();
	let mut scan = InputScanner::new(stdin.lock(),256);

	let s:Vec<char> = scan.next::<String>().chars().collect();

	let mut dp:Vec<usize> = vec![0; s.len()+1];
	let mut iwi:Vec<(usize,usize)> = vec![(0,0); s.len()+1];

	for i in 0..s.len() {
		if i >= 2 && s[i] == 'i' {
			if s[i-1] == 'w' {
				let (count,left_bound) = iwi[i-1];

				let offset = if count * 3 + 2 <= i - left_bound {
					count * 3 + 2
				} else {
					2
				};

				if count > 0 && count * 3 + 2 <= i - left_bound && s[i-offset] == 'i' {
					iwi[i+1] = (iwi[i-1].0+1,iwi[i-1].1);
					dp[i+1] = std::cmp::max(dp[i], dp[i-offset] + iwi[i+1].0);
				} else if s[i-2] == 'i' && count > 0 {
					iwi[i+1] = (iwi[i-2].0+1,iwi[i-2].1);
					dp[i+1] = std::cmp::max(dp[i], dp[i-2] + iwi[i+1].0);
				} else if s[i-2] == 'i' {
					iwi[i+1] = (1,i-2);
					dp[i+1] = std::cmp::max(dp[i], dp[i-2] + iwi[i+1].0);
				} else {
					dp[i+1] = dp[i];
				}
			} else {
				let (count,left_bound) = iwi[i];

				let offset = if count * 3 + 2 <= i - left_bound {
					count * 3 + 2
				} else {
					2
				};

				if count * 3 + 2 <= i - left_bound && s[i-offset] == 'i' && s[i-offset+1] == 'w' {
					iwi[i+1] = (iwi[i].0 + 1,iwi[i].1);
					dp[i + 1] = std::cmp::max(dp[i], dp[i - offset] + iwi[i + 1].0);
				} else {
					dp[i+1] = dp[i];
				}
			}
		} else {
			dp[i+1] = dp[i];
		}
	}

	println!("{}",dp[s.len()]);
}
struct InputScanner<R: BufRead> {
	reader: R,
	buf: Vec<u8>, // Should never be empty
	pos: usize,   // Should never be out of bounds as long as the input ends with '\n'
}
impl<R: BufRead> InputScanner<R> {
	fn new(reader: R, capacity: usize) -> Self {
		InputScanner {
			reader: reader,
			buf: Vec::with_capacity(capacity),
			pos: 0,
		}
	}

	#[inline]
	fn next<T: FromStr>(&mut self) -> T
		where
			T::Err: fmt::Debug,
	{
		if self.buf.is_empty() {
			self._read_next_line();
		}
		let mut start = None;
		while self.pos < self.buf.len() {
			match (self.buf[self.pos], start.is_some()) {
				(b' ', true) | (b'\n', true) | (b'\r', true) => break,
				(_, true) | (b' ', false) => self.pos += 1,
				(b'\n', false) | (b'\r', false) => self._read_next_line(),
				(_, false) => start = Some(self.pos),
			}
		}
		let target = &self.buf[start.unwrap()..self.pos];
		unsafe { str::from_utf8_unchecked(target) }.parse().unwrap()
	}

	#[inline]
	fn _read_next_line(&mut self) {
		self.pos = 0;
		self.buf.clear();
		if self.reader.read_until(b'\n', &mut self.buf).unwrap() == 0 {
			panic!("Reached EOF");
		}
	}
}

Submission Info

Submission Time
Task I - イウィ
User jinpu
Language Rust (1.42.0)
Score 0
Code Size 2731 Byte
Status WA
Exec Time 7 ms
Memory 2188 KB

Judge Result

Set Name All
Score / Max Score 0 / 5
Status
AC × 3
WA × 4
Set Name Test Cases
All 00, 01, 02, 03, 04, 90, 91
Case Name Status Exec Time Memory
00 WA 7 ms 2188 KB
01 WA 2 ms 2044 KB
02 AC 2 ms 2016 KB
03 WA 2 ms 2040 KB
04 WA 2 ms 2048 KB
90 AC 2 ms 1912 KB
91 AC 2 ms 2112 KB