1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#![allow(non_upper_case_globals)]
use std::*;
use error::Error;
use io;
use std::str::FromStr;
pub type Er = anyhow::Error;
pub type Res<T> = anyhow::Result<T>;
pub type Maybe = Res<()>;
pub const ok: Maybe = Ok(());
pub fn cin<T>() -> Res<T>
where T: FromStr, T::Err: Error + Send + Sync + 'static {
let mut input = String::new();
io::stdin().read_line(&mut input)?;
Ok(input.trim().parse()?)
}
pub fn print<T: fmt::Display>(output: T) {
println!("{output}");
}
pub fn debug<T: fmt::Debug>(output: T) {
println!("{output:?}");
}
pub trait Utf8Container {
fn slice(&self, start: usize, end: usize) -> Option<&str>;
fn from(&self, start: usize) -> Option<&str>;
fn to(&self, end: usize) -> Option<&str>;
}
impl Utf8Container for str {
fn slice(&self, start: usize, end: usize) -> Option<&str> {
let start = self.char_indices().nth(start)?.0;
let end = self.char_indices().nth(end)?.0;
Some(&self[start..end])
}
fn from(&self, start: usize) -> Option<&str> {
let start = self.char_indices().nth(start)?.0;
Some(&self[start..])
}
fn to(&self, end: usize) -> Option<&str> {
let end = self.char_indices().nth(end)?.0;
Some(&self[..end])
}
}
impl Utf8Container for String {
fn slice(&self, start: usize, end: usize) -> Option<&str> {
let start = self.char_indices().nth(start)?.0;
let end = self.char_indices().nth(end)?.0;
Some(&self[start..end])
}
fn from(&self, start: usize) -> Option<&str> {
let start = self.char_indices().nth(start)?.0;
Some(&self[start..])
}
fn to(&self, end: usize) -> Option<&str> {
let end = self.char_indices().nth(end)?.0;
Some(&self[..end])
}
}