Advertisement
beephsupreme

spnauti

Sep 13th, 2022
1,166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.38 KB | None | 0 0
  1. //spnauti-rusT {{{
  2. #[allow(unused_imports)]
  3. use std::cell::*;
  4. #[allow(unused_imports)]
  5. use std::cmp::Ordering::{self, *};
  6. #[allow(unused_imports)]
  7. use std::collections::*;
  8. use std::fmt::Debug;
  9. use std::io::*;
  10. #[allow(unused_imports)]
  11. use std::iter::{self, *};
  12. #[allow(unused_imports)]
  13. use std::ops::{self, *};
  14. use std::str::{self, *};
  15. #[allow(unused_macros)]
  16. macro_rules! m {
  17.     ($c:tt,$x:expr,$y:expr) => {{
  18.         let b = $y;
  19.         let a = &mut$x;
  20.         if b$c * a {
  21.             *a = b;
  22.             true
  23.         } else {
  24.             false
  25.         }
  26.     }};
  27. }
  28. #[allow(unused_macros)]
  29. macro_rules! l {
  30.     ($($v:ident),+ =$e:expr) => {$(let$v=$e;)+};
  31.     ($($v:ident),+:$t:ty=$e:expr) => {$(let$v:$t=$e;)+};
  32.     (mut $($v:ident),+ =$e:expr) => {$(let mut$v=$e;)+};
  33.     (mut $($v:ident),+:$t:ty=$e:expr) => {$(let mut$v:$t=$e;)+};
  34. }
  35. #[allow(unused_macros)]
  36. macro_rules! rep { {[$c:expr]$($s:tt)+} => {for _ in 0..$c {$($s)+}} }
  37. #[allow(unused_macros)]
  38. fn reader() -> WordReaderC {
  39.     WordReaderC::new()
  40. }
  41. #[allow(unused_macros)]
  42. fn writer() -> BufWriter<Stdout> {
  43.     BufWriter::new(stdout())
  44. }
  45. struct WordReaderC {
  46.     buf: Vec<u8>,
  47.     pos: usize,
  48.     q: std::io::StdinLock<'static>,
  49. } //'
  50. #[allow(dead_code)]
  51. impl WordReaderC {
  52.     fn new() -> Self {
  53.         let r = unsafe { &*Box::into_raw(Box::new(stdin())) };
  54.         Self {
  55.             q: r.lock(),
  56.             buf: Vec::new(),
  57.             pos: 0,
  58.         }
  59.     }
  60.     fn next_line(&mut self) -> bool {
  61.         self.buf.clear();
  62.         self.pos = 0;
  63.         self.q.read_until(b'\n', &mut self.buf).unwrap_or(0) > 0
  64.     }
  65.     fn is_ws(c: u8) -> bool {
  66.         c == b' ' || c == b'\r' || c == b'\n' || c == b'\t'
  67.     }
  68.     fn byte(&mut self) -> Option<u8> {
  69.         if self.pos == self.buf.len() {
  70.             if !self.next_line() {
  71.                 return None;
  72.             }
  73.         }
  74.         self.pos += 1;
  75.         Some(self.buf[self.pos - 1])
  76.     }
  77.     fn vb(&mut self) -> Vec<u8> {
  78.         let mut s = Vec::with_capacity(8);
  79.         let mut f = false;
  80.         loop {
  81.             if let Some(c) = self.byte() {
  82.                 if !Self::is_ws(c) {
  83.                     s.push(c);
  84.                     f = true;
  85.                 } else if f {
  86.                     break;
  87.                 }
  88.             } else {
  89.                 break;
  90.             }
  91.         }
  92.         s
  93.     }
  94.     fn s(&mut self) -> String {
  95.         String::from_utf8(self.vb()).expect("invalid utf8")
  96.     }
  97.     fn i(&mut self) -> i32 {
  98.         self.p()
  99.     }
  100.     fn l(&mut self) -> i64 {
  101.         self.p()
  102.     }
  103.     fn u(&mut self) -> usize {
  104.         self.p()
  105.     }
  106.     fn f(&mut self) -> f64 {
  107.         self.p()
  108.     }
  109.     fn vi(&mut self, n: usize) -> Vec<i32> {
  110.         self.vp(n)
  111.     }
  112.     fn vl(&mut self, n: usize) -> Vec<i64> {
  113.         self.vp(n)
  114.     }
  115.     fn vu(&mut self, n: usize) -> Vec<usize> {
  116.         self.vp(n)
  117.     }
  118.     fn ii(&mut self, n: usize) -> impl Iterator<Item = i32> {
  119.         self.ip(n).into_iter()
  120.     }
  121.     fn iu(&mut self, n: usize) -> impl Iterator<Item = usize> {
  122.         self.ip(n).into_iter()
  123.     }
  124.     fn p<T: FromStr>(&mut self) -> T
  125.     where
  126.         T::Err: Debug,
  127.     {
  128.         let w = self.vb();
  129.         str::from_utf8(w.as_ref()).unwrap().parse::<T>().unwrap()
  130.     }
  131.     fn vp<T: FromStr>(&mut self, n: usize) -> Vec<T>
  132.     where
  133.         T::Err: Debug,
  134.     {
  135.         (0..n).map(|_| self.p()).collect()
  136.     }
  137.     fn ip<T: FromStr>(&mut self, n: usize) -> impl Iterator<Item = T>
  138.     where
  139.         T::Err: Debug,
  140.     {
  141.         self.vp(n).into_iter()
  142.     }
  143. }
  144. //------------------- End rusT }}}
  145.  
  146. fn next_permutation<T: Ord + Debug>(a: &mut [T]) -> bool {
  147.     let n = a.len();
  148.     for i in (0..n - 1).rev() {
  149.         if a[i] < a[i + 1] {
  150.             for j in (i + 1..n).rev() {
  151.                 if a[i] < a[j] {
  152.                     a.swap(i, j);
  153.                     break;
  154.                 }
  155.             }
  156.             a[i + 1..n].reverse();
  157.             return true;
  158.         }
  159.     }
  160.     false
  161. }
  162.  
  163. fn main() {
  164.     let mut rin = reader();
  165.     let mut rout = writer();
  166.  
  167.     let mut s = rin.vb();
  168.     s.sort();
  169.     let mut sol = Vec::new();
  170.     sol.push(s.clone());
  171.     while next_permutation(&mut s) {
  172.         sol.push(s.clone());
  173.     }
  174.     writeln!(rout, "{}", sol.len()).ok();
  175.     for a in sol {
  176.         writeln!(rout, "{}", String::from_utf8(a).unwrap()).ok();
  177.     }
  178. }
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement