algorist 0.10.2

Algorithms and data structures for competitive programming
Documentation
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#![allow(clippy::needless_doctest_main)]

//! This module features utilities for reading input using the [`Scanner`], and
//! writing output using the [`Writer`] and [`macro@wln`] macro.
//!
//! # Examples
//!
//! You will typically use the [`test_cases()`] or [`test_case()`] functions, to
//! obtain a [`Scanner`] and a [`Writer`] when solving contest problems.
//!
//! ## Reading input of a single test
//!
//! It is less common nowadays, as most contest will provide you with several
//! test cases, but you can still do it, if you need to:
//!
//! ``` bash
//! # Input:
//! 2 3
//!
//! # Output:
//! Sum: 5
//! ```
//!
//! ``` no_run
//! use algorist::io::{test_case, wln};
//!
//! fn main() {
//!     test_case(&mut |scan, w| {
//!         let (a, b): (i32, i32) = scan.pair();
//!         wln!(w, "Sum: {}", a + b);
//!     });
//! }
//! ```
//!
//! ## Reading number of test cases and processing them
//!
//! Normally, in contest programming, you will have several test cases to
//! process.
//!
//! So, the input will start with a single integer `t`, which is the number of
//! test cases, followed by `t` test case inputs.
//!
//! ``` bash
//! # Input:
//! 2
//! 3 2
//! 1 2
//!
//! # Output:
//! Sum: 5
//! Sum: 3
//! ```
//!
//! ``` no_run
//! use algorist::io::{test_cases, wln};
//!
//! fn main() {
//!     test_cases(&mut |scan, w| {
//!         let (a, b): (i32, i32) = scan.pair();
//!         wln!(w, "Sum: {}", a + b);
//!     });
//! }
//! ```
//!
//! As you can see, the difference between reading a single test case and
//! reading multiple test cases is minimal -- you just need to call different
//! function, with the same closure.

use std::{
    collections::VecDeque,
    io::{self, BufWriter, StdinLock, StdoutLock, Write, prelude::*},
};

/// A helper function to read multiple test cases from standard input, and write
/// output to standard output.
///
/// # Example
///
/// ``` no_run
/// use algorist::io::{test_cases, wln};
///
/// // `test_cases` will read a `t` value from input, and then call the
/// // provided closure `t` times, allowing you to process each test case.
/// test_cases(&mut |scan, w| {
///     // You can use `u2()` to read a pair of `usize` values.
///     let a = scan.u(); // Read a single `usize`
///     let b = scan.u(); // Read another `usize`
///
///     wln!(w, "Sum: {}", a + b); // Write the sum to output
/// });
/// ```
///
/// ``` bash
/// # Input:
/// 2
/// 3 2
/// 2 1
///
/// # Output:
/// Sum: 5
/// Sum: 3
/// ```
///
/// In case you want to read a single test case, use the [`test_case()`],
/// instead.
pub fn test_cases<F: FnMut(&mut Scanner<StdinLock>, &mut Writer<BufWriter<StdoutLock>>)>(
    f: &mut F,
) {
    let mut scan = Scanner::new(io::stdin().lock());
    let mut w = Writer::new(io::BufWriter::new(io::stdout().lock()));

    scan.test_cases(&mut |scan| {
        f(scan, &mut w);
    });
}

/// A helper function to read a single test case from standard input, and write
/// to standard output.
///
/// # Example
///
/// ``` no_run
/// use algorist::io::{test_case, wln};
///
/// test_case(&mut |scan, w| {
///     // You can use `u2()` to read a pair of `usize` values.
///     let a = scan.u(); // Read a single `usize`
///     let b = scan.u(); // Read another `usize`
///
///     wln!(w, "Sum: {}", a + b); // Write the sum to output
/// });
/// ```
///
/// ``` bash
/// # Input:
/// 3 2
///
/// # Output:
/// Sum: 5
/// ```
pub fn test_case<F: FnMut(&mut Scanner<StdinLock>, &mut Writer<BufWriter<StdoutLock>>)>(f: &mut F) {
    let mut scan = Scanner::new(io::stdin().lock());
    let mut w = Writer::new(io::BufWriter::new(io::stdout().lock()));
    f(&mut scan, &mut w);
}

/// A `Writer` is a wrapper around `BufWriter<W>` that provides a convenient
/// interface for writing formatted output, without requiring to import
/// `std::io::Write` by the client code. It is expected to be used with [`wln!`]
/// macro.
///
/// # Example
///
/// ```no_run
/// use {
///     algorist::io::{Writer, wln},
///     std::io,
/// };
///
/// let mut w = Writer::new(io::BufWriter::new(io::stdout().lock()));
/// wln!(w, "Hello, {}!", "world");
/// writeln!(w, "This is a test."); // `wln!` is shorter and more ergonomic
/// ```
pub struct Writer<W: Write>(BufWriter<W>);

impl<W: Write> Writer<W> {
    pub fn new(inner: W) -> Self {
        Self(BufWriter::new(inner))
    }

    /// Writes a formatted string to the underlying writer.
    pub fn write_fmt(&mut self, args: std::fmt::Arguments) {
        let _ = self.0.write_fmt(args);
    }

    /// Flushes the underlying writer, ensuring all buffered data is written
    /// out.
    pub fn flush(&mut self) {
        let _ = self.0.flush();
    }
}

/// Scanner reads buffered input and parses it into tokens.
///
/// The `Scanner` is designed to simplify reading input in competitive
/// programming. It allows you to read various types of data from standard input
/// efficiently. Moreover, it supports several most common operations like
/// reading several test cases, strings, and vectors.
///
/// ## Reading input of a single test
///
/// It is less common nowadays, as most contest will provide you with several
/// test cases, but you can still read a single test case's input, if you need
/// to:
///
/// ``` bash
/// # Input:
/// 1 2 3 4
/// ```
///
/// ``` no_run
/// use {algorist::io::Scanner, std::io};
///
/// let mut scan = Scanner::new(io::stdin().lock());
///
/// let a: i32 = scan.next(); // reads a single token and parses it as `i32`
/// let a: i32 = scan.i(); // same as above
/// ```
///
/// ## Reading input from several test cases
///
/// Normally, in contest programming, you will have several test cases to
/// process. So, the input will start with a single integer `t`, which is the
/// number of test cases, followed by `t` test inputs.
///
/// `Scanner` provides a convenient method
/// [`test_cases()`](Scanner::test_cases) to read and process multiple tests
/// more ergonomically.
///
/// ``` bash
/// # Input:
/// 1
/// 3
/// 1 2 3
/// ```
///
/// ``` no_run
/// use {
///     algorist::io::{Scanner, wln},
///     std::io::{self, Write},
/// };
///
/// // Initialize a `Scanner` for reading input and a `BufWriter` for output.
/// let mut scan = Scanner::new(io::stdin().lock());
/// let mut w = io::BufWriter::new(io::stdout().lock());
///
/// // Read multiple test cases and process them.
/// scan.test_cases(&mut |scan| {
///     // Each test case reads a `usize` into `n` and then a vector of `n` integers.
///     let n = scan.u();
///     let vals: Vec<i32> = scan.vec(n);
///
///     // Simple wrapper around `writeln!` for convenience.
///     wln!(w, "{}", vals.len());
/// });
/// ```
///
/// # Important Note
///
/// For even more ergonomic usage, rely on the stand-alone [`test_cases()`]
/// which will create a [`Scanner`] and a [`Writer`] for you, and pass them to
/// the closure.
pub struct Scanner<R> {
    reader: R,
    buffer: Vec<u8>,
    iter: std::str::SplitWhitespace<'static>,
}

impl<R: BufRead> Scanner<R> {
    /// Creates a new `Scanner` instance with the given reader.
    ///
    /// # Example
    ///
    /// ``` no_run
    /// use {
    ///     algorist::io::Scanner,
    ///     std::io::{self, BufReader, Write},
    /// };
    ///
    /// // Read from standard input.
    /// let mut scan = Scanner::new(io::stdin().lock());
    ///
    /// // Write to standard output.
    /// let mut w = io::BufWriter::new(io::stdout().lock());
    ///
    /// let n: u16 = scan.next(); // Reads the next token as a `u16`.
    /// writeln!(w, "{}", n).unwrap(); // Writes the value to output.
    /// ```
    pub fn new(reader: R) -> Self {
        Self {
            reader,
            buffer: Vec::new(),
            iter: "".split_whitespace(),
        }
    }

    /// Reads the next token from the input, parsing it into the specified `T`.
    ///
    /// This method will read until a newline character is encountered, then
    /// split the line into whitespace-separated tokens, and traverse the
    /// iterator.
    ///
    /// It will return the next token as type `T`.
    ///
    /// # Example
    ///
    /// ```
    /// use {algorist::io::Scanner, std::io::BufReader};
    ///
    /// let input = b"42 3.14 hello\n";
    /// let mut scan = Scanner::new(BufReader::new(input.as_ref()));
    /// let x: i32 = scan.next();
    /// let y: f64 = scan.next();
    /// let s: String = scan.next();
    /// assert_eq!(x, 42);
    /// assert_eq!(y, 3.14);
    /// assert_eq!(s, "hello");
    /// ```
    #[allow(clippy::should_implement_trait)]
    pub fn next<T: std::str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buffer.clear();
            self.reader
                .read_until(0xA, &mut self.buffer)
                .expect("Failed read");

            self.iter = unsafe {
                let slice = std::str::from_utf8_unchecked(&self.buffer);
                std::mem::transmute::<std::str::SplitWhitespace<'_>, std::str::SplitWhitespace<'_>>(
                    slice.split_whitespace(),
                )
            };
        }
    }

    /// Reads multiple test cases from the input, applying the provided function
    /// `f` to each test case.
    ///
    /// Normally, in contest problems, the first token read is the number of
    /// test cases `t`, and the function `f` is called `t` times, allowing
    /// you to process each test case individually.
    ///
    /// # Example
    ///
    /// ```
    /// use {algorist::io::Scanner, std::io::BufReader};
    ///
    /// let input = b"2\n1 2\n3 4\n";
    /// let mut scan = Scanner::new(BufReader::new(input.as_ref()));
    /// let mut sum = 0;
    /// scan.test_cases(&mut |scan| {
    ///     let x: i32 = scan.next();
    ///     let y: i32 = scan.next();
    ///     sum += x + y;
    /// });
    /// assert_eq!(sum, 10);
    /// ```
    pub fn test_cases<F: FnMut(&mut Self)>(&mut self, f: &mut F) {
        let t = self.u();
        for _ in 0..t {
            f(self);
        }
    }

    /// Reads the next token as a `usize`.
    ///
    /// # Example
    ///
    /// ```
    /// use {algorist::io::Scanner, std::io::BufReader};
    ///
    /// let input = b"42\n";
    /// let mut scan = Scanner::new(BufReader::new(input.as_ref()));
    /// let x: usize = scan.u();
    /// assert_eq!(x, 42);
    /// ```
    pub fn u(&mut self) -> usize {
        self.next()
    }

    /// Reads pair of `usize` values.
    ///
    /// # Example
    ///
    /// ```
    /// use {algorist::io::Scanner, std::io::BufReader};
    ///
    /// let input = b"42 43\n";
    /// let mut scan = Scanner::new(BufReader::new(input.as_ref()));
    /// let (x, y): (usize, usize) = scan.u2();
    /// assert_eq!(x, 42);
    /// assert_eq!(y, 43);
    /// ```
    pub fn u2(&mut self) -> (usize, usize) {
        (self.u(), self.u())
    }

    /// Reads triplet of `usize` values.
    ///
    /// See also [`u2`](Scanner::u2).
    pub fn u3(&mut self) -> (usize, usize, usize) {
        (self.u(), self.u(), self.u())
    }

    /// Reads quadruplet of `usize` values.
    ///
    /// See also [`u2`](Scanner::u2).
    pub fn u4(&mut self) -> (usize, usize, usize, usize) {
        (self.u(), self.u(), self.u(), self.u())
    }

    /// Reads the next token as an `i32`.
    pub fn i(&mut self) -> i32 {
        self.next()
    }

    /// Reads pair of `i32` values.
    pub fn i2(&mut self) -> (i32, i32) {
        (self.i(), self.i())
    }

    /// Reads triplet of `i32` values.
    pub fn i3(&mut self) -> (i32, i32, i32) {
        (self.i(), self.i(), self.i())
    }

    /// Reads quadruplet of `i32` values.
    pub fn i4(&mut self) -> (i32, i32, i32, i32) {
        (self.i(), self.i(), self.i(), self.i())
    }

    /// Reads pair of values of type `T`.
    ///
    /// # Example
    /// ```
    /// use {algorist::io::Scanner, std::io::BufReader};
    ///
    /// let input = b"1 2\n";
    /// let mut scan = Scanner::new(BufReader::new(input.as_ref()));
    /// let (x, y): (i32, i32) = scan.pair();
    /// assert_eq!(x + y, 3);
    ///
    /// let input = b"foo bar\n";
    /// let mut scan = Scanner::new(BufReader::new(input.as_ref()));
    /// let (x, y): (String, String) = scan.pair();
    /// assert_eq!(x, "foo");
    /// assert_eq!(y, "bar");
    /// ```
    pub fn pair<T: std::str::FromStr>(&mut self) -> (T, T) {
        (self.next(), self.next())
    }

    /// Reads triplet of values of type `T`.
    ///
    /// See also [`pair`](Scanner::pair).
    pub fn triplet<T: std::str::FromStr>(&mut self) -> (T, T, T) {
        (self.next(), self.next(), self.next())
    }

    /// Gets the next token as a `String`.
    pub fn string(&mut self) -> String {
        self.next()
    }

    /// Gets the next token as `Vec<u8>`.
    pub fn bytes(&mut self) -> Vec<u8> {
        self.string().bytes().collect()
    }

    /// Gets the next token as `Vec<char>`.
    pub fn chars(&mut self) -> Vec<char> {
        self.string().chars().collect()
    }

    /// Reads a vector of `T` from the input, where `n` is the number of
    /// elements.
    ///
    /// # Example
    /// ```
    /// use {algorist::io::Scanner, std::io::BufReader};
    ///
    /// let input = b"1 2 3\n";
    /// let mut scan = Scanner::new(BufReader::new(input.as_ref()));
    /// let v: Vec<i32> = scan.vec(3);
    /// assert_eq!(v, vec![1, 2, 3]);
    /// ```
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        let mut result = Vec::with_capacity(n);
        (0..n).for_each(|_| result.push(self.next()));
        result
    }

    /// Reads a vector of `T` from the input, where `n` is the number of
    /// elements, and the first element is a default value for `T`.
    ///
    /// # Example
    /// ```
    /// use {algorist::io::Scanner, std::io::BufReader};
    ///
    /// let input = b"1 2 3\n";
    /// let mut scan = Scanner::new(BufReader::new(input.as_ref()));
    /// let v: Vec<i32> = scan.vec_padded(3);
    /// assert_eq!(v, vec![0, 1, 2, 3]);
    /// ```
    pub fn vec_padded<T: std::str::FromStr + Default>(&mut self, n: usize) -> Vec<T> {
        let mut result = Vec::with_capacity(n + 1);
        result.push(T::default());
        (0..n).for_each(|_| result.push(self.next()));
        result
    }

    /// Reads a `VecDeque<T>` from the input, where `n` is the number of
    /// elements to read.
    ///
    /// # Example
    /// ```
    /// use {
    ///     algorist::io::Scanner,
    ///     std::{collections::VecDeque, io::BufReader},
    /// };
    ///
    /// let input = b"1 2 3\n";
    /// let mut scan = Scanner::new(BufReader::new(input.as_ref()));
    /// let v: VecDeque<i32> = scan.vec_deque(3);
    /// assert_eq!(v, VecDeque::from(vec![1, 2, 3]));
    /// ```
    pub fn vec_deque<T: std::str::FromStr>(&mut self, n: usize) -> VecDeque<T> {
        let mut result = VecDeque::with_capacity(n);
        (0..n).for_each(|_| result.push_back(self.next()));
        result
    }

    /// Reads a `HashSet<T>` from the input, where `n` is the number of
    /// elements to read.
    ///
    /// # Example
    /// ```
    /// use {
    ///     algorist::io::Scanner,
    ///     std::{collections::HashSet, io::BufReader},
    /// };
    ///
    /// let input = b"1 2 3\n";
    /// let mut scan = Scanner::new(BufReader::new(input.as_ref()));
    /// let set: HashSet<i32> = scan.hash_set(3);
    /// assert_eq!(set, HashSet::from([1, 2, 3]));
    /// ```
    pub fn hash_set<T: std::hash::Hash + std::cmp::Eq + std::str::FromStr>(
        &mut self,
        n: usize,
    ) -> std::collections::HashSet<T> {
        let mut result = std::collections::HashSet::with_capacity(n);
        (0..n).for_each(|_| {
            result.insert(self.next());
        });
        result
    }
}

fn wv<W: Write, T: std::fmt::Display>(w: &mut W, v: &[T]) {
    write!(
        w,
        "{}",
        v.iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>()
            .join(" ")
    )
    .unwrap();
}

/// A macro for writing a line with formatted output.
///
/// Just like `writeln!()`, but with a shorter name, and no return value (so,
/// now warning about unused result).
///
/// # Example
/// ```
/// use {
///     algorist::io::wln,
///     std::io::{self, Write},
/// };
///
/// let mut w = io::BufWriter::new(io::stdout().lock());
///
/// // Using more ergonomic `wln!` macro:
/// wln!(w, "Hello, {}!", "world");
///
/// // Alternatively, using the `writeln!()` macro directly:
/// let _ = writeln!(w, "Hello, {}!", "world");
/// ```
#[macro_export]
macro_rules! wln_impl {
    ($($es:expr),+) => {{
        let _ = writeln!($($es),+);
    }}
}
pub use wln_impl as wln;

pub fn wvln<W: Write, T: std::fmt::Display>(w: &mut W, v: &[T]) {
    wv(w, v);
    writeln!(w).ok();
}

#[cfg(test)]
mod tests {
    use {super::*, crate::io::Scanner, std::io::BufReader};

    #[test]
    fn read_test_cases() {
        let input = b"2\n1 2\n3 4\n";
        let mut scanner = Scanner::new(BufReader::new(input.as_ref()));
        let mut sum = 0;
        scanner.test_cases(&mut |scanner| {
            let x: i32 = scanner.next();
            let y: i32 = scanner.next();
            sum += x + y;
        });
        assert_eq!(sum, 10);
    }

    #[test]
    fn read_i32_list() {
        let input = b"1 2\n";
        let mut scanner = Scanner::new(BufReader::new(input.as_ref()));

        let x: i32 = scanner.next();
        let y: i32 = scanner.next();
        assert_eq!(x + y, 3);
    }

    #[test]
    fn read_usize_list() {
        let input = b"1 2\n";
        let mut scanner = Scanner::new(BufReader::new(input.as_ref()));
        let (x, y) = scanner.u2();
        assert_eq!(x + y, 3);

        let input = b"1 2 3\n";
        let mut scanner = Scanner::new(BufReader::new(input.as_ref()));
        let (x, y, z) = scanner.u3();
        assert_eq!(x + y + z, 6);
    }

    #[test]
    fn read_pair() {
        let input = b"1 2\n";
        let mut scanner = Scanner::new(BufReader::new(input.as_ref()));
        let (x, y): (i32, i32) = scanner.pair();
        assert_eq!(x + y, 3);
    }

    #[test]
    fn read_triplet() {
        let input = b"1 2 3\n";
        let mut scanner = Scanner::new(BufReader::new(input.as_ref()));
        let (x, y, z): (i32, i32, i32) = scanner.triplet();
        assert_eq!(x + y + z, 6);
    }

    #[test]
    fn read_string() {
        let input = b"hello\nworld\n";
        let mut scanner = Scanner::new(BufReader::new(input.as_ref()));
        let s: String = scanner.string();
        assert_eq!(s, "hello");
        let s: String = scanner.string();
        assert_eq!(s, "world");
    }

    #[test]
    fn read_byte_list() {
        let input = b"abc\n";
        let mut scanner = Scanner::new(BufReader::new(input.as_ref()));
        let bytes: Vec<u8> = scanner.bytes();
        assert_eq!(bytes, vec![b'a', b'b', b'c']);
    }

    #[test]
    fn read_char_list() {
        let input = b"abc\n";
        let mut scanner = Scanner::new(BufReader::new(input.as_ref()));
        let chars: Vec<char> = scanner.chars();
        assert_eq!(chars, vec!['a', 'b', 'c']);
    }

    #[test]
    fn read_vec() {
        let input = b"1 2 3\n";
        let mut scanner = Scanner::new(BufReader::new(input.as_ref()));
        let v: Vec<i32> = scanner.vec(3);
        assert_eq!(v, vec![1, 2, 3]);

        let input = b"1 2 3\n";
        let mut scanner = Scanner::new(BufReader::new(input.as_ref()));
        let v: Vec<i32> = scanner.vec_padded(3);
        assert_eq!(v, vec![0, 1, 2, 3]);
    }

    #[test]
    fn write_vec() {
        let mut output = Vec::new();
        wv(&mut output, &vec![1, 2, 3]);
        assert_eq!(output, b"1 2 3");
    }
}