Skip to content

Commit 0591e37

Browse files
committed
Modify some of the existing examples
1 parent 285b298 commit 0591e37

File tree

4 files changed

+64
-44
lines changed

4 files changed

+64
-44
lines changed

examples/practice-b-naive.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
// https://atcoder.jp/contests/practice/tasks/practice_2
2+
//
3+
// 以下のクレートを使用。
4+
//
5+
// - `itertools`
6+
// - `maplit`
27

38
use maplit::hashset;
4-
59
use std::{io, str};
610

711
fn main() {
8-
fn read_line() -> String {
9-
let mut input = "".to_owned();
10-
io::stdin().read_line(&mut input).unwrap();
11-
input
12-
}
12+
let mut input = "".split_ascii_whitespace();
13+
let mut read = || loop {
14+
if let Some(word) = input.next() {
15+
break word;
16+
}
17+
input = {
18+
let mut input = "".to_owned();
19+
io::stdin().read_line(&mut input).unwrap();
20+
if input.is_empty() {
21+
panic!("reached EOF");
22+
}
23+
Box::leak(input.into_boxed_str()).split_ascii_whitespace()
24+
};
25+
};
26+
macro_rules! read(($ty:ty) => (read().parse::<$ty>().unwrap()));
1327

14-
fn query(l: u8, r: u8) -> bool {
15-
println!("? {} {}", l as char, r as char);
16-
read_line() == "<\n"
17-
}
28+
let (n, _): (u32, u32) = (read!(u32), read!(u32));
1829

19-
let n = {
20-
let input = read_line();
21-
let mut input = input.split_whitespace();
22-
macro_rules! read(($ty:ty) => (input.next().unwrap().parse::<$ty>().unwrap()));
23-
read!(u32)
30+
let query = |l: u8, r: u8| -> bool {
31+
println!("? {} {}", l as char, r as char);
32+
read!(char) == '<'
2433
};
2534

2635
let ans = match n {

examples/practice-b-proconio.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
// https://atcoder.jp/contests/practice/tasks/practice_2
2+
//
3+
// 以下のクレートを使用。
4+
//
5+
// - `itertools`
6+
// - `maplit`
7+
// - `proconio`
28

39
use maplit::hashset;
4-
use proconio::input;
5-
use proconio::source::line::LineSource;
6-
7-
use std::{io, str};
10+
use proconio::{input, source::line::LineSource};
11+
use std::{
12+
io::{self, BufReader},
13+
str,
14+
};
815

916
fn main() {
10-
let stdin = io::stdin();
11-
let mut stdin = stdin.lock();
17+
// `proconio::input!`はrelease modeではデフォルトでは`OnceSource`を使ってしまうので、
18+
// `input! { from: .., }`で`LineSource`を指定する必要がある。
19+
// `Source`を指定するときはこのように部分適用したマクロを用意しておくと楽。
20+
//
21+
// https://docs.rs/proconio/0.3.6/proconio/macro.input.html
22+
// https://docs.rs/proconio/0.3.6/proconio/source/once/struct.OnceSource.html
23+
// https://docs.rs/proconio/0.3.6/proconio/source/line/struct.LineSource.html
24+
// https://docs.rs/proconio/0.3.6/proconio/source/trait.Source.html
25+
let mut stdin = LineSource::new(BufReader::new(io::stdin()));
26+
macro_rules! input(($($tt:tt)*) => (proconio::input!(from &mut stdin, $($tt)*)));
1227

13-
input! {
14-
from LineSource::new(&mut stdin),
15-
n: u32,
16-
}
28+
input!(n: u32, _: u32);
1729

1830
let query = |l: u8, r: u8| -> _ {
1931
println!("? {} {}", l as char, r as char);
20-
input! {
21-
from LineSource::new(&mut stdin),
22-
c: char,
23-
}
32+
input!(c: char);
2433
c == '<'
2534
};
2635

examples/practice-b-text-io.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
// https://atcoder.jp/contests/practice/tasks/practice_2
2+
//
3+
// 以下のクレートを使用。
4+
//
5+
// - `itertools`
6+
// - `maplit`
7+
// - `text_io`
28

39
use maplit::hashset;
10+
use std::str;
411
use text_io::read;
512

6-
use std::{io, str};
7-
813
fn main() {
9-
fn read_line() -> String {
10-
let mut input = "".to_owned();
11-
io::stdin().read_line(&mut input).unwrap();
12-
input
13-
}
14-
15-
let n: u32 = {
16-
let line = read_line();
17-
read!("{}", line.bytes())
18-
};
14+
let (n, _): (u32, u32) = (read!(), read!());
1915

2016
let query = |l: u8, r: u8| -> _ {
2117
println!("? {} {}", l as char, r as char);
22-
read_line() == "<\n"
18+
let c: char = read!();
19+
c == '<'
2320
};
2421

2522
let ans = match n {

examples/practice-b-whiteread.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
// https://atcoder.jp/contests/practice/tasks/practice_2
2+
//
3+
// 以下のクレートを使用。
4+
//
5+
// - `itertools`
6+
// - `maplit`
7+
// - `whiteread`
28

39
use maplit::hashset;
4-
use whiteread::Reader;
5-
610
use std::str;
11+
use whiteread::Reader;
712

813
fn main() {
914
let mut rdr = Reader::from_stdin_naive();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy