diff --git a/038/main.rs b/038/main.rs new file mode 100644 index 0000000..435325f --- /dev/null +++ b/038/main.rs @@ -0,0 +1,106 @@ +use std::collections::HashSet; +use std::collections::VecDeque; + +type Str = VecDeque; +type Res = HashSet; + +fn take(n: usize, s: &mut Str) -> u32 { + let mut res = 0u32; + for _ in 0..n { + res *= 10; + res += s.pop_front().unwrap() as u32; + } + res +} + +fn nstr(n: u32) -> Str { + let mut res = Str::new(); + let mut nn = n; + while nn > 0 { + res.push_front((nn % 10) as u8); + nn /= 10; + } + res +} + +fn strn(s: &Str) -> u32 { + let mut res = 0; + for d in s.iter() { + res *= 10; + res += *d as u32; + } + res +} + +fn len(n: u32) -> usize { + let mut res = 0usize; + let mut nn = n; + while nn > 0 { + res += 1; + nn /= 10; + } + res +} + +fn testn(s: &Str, n: usize) -> bool { + let mut ss = s.clone(); + let k = take(n, &mut ss); + let mut i = 2; + while len(k * i) <= ss.len() { + let kt = take(len(k*i), &mut ss); + if kt != k*i { return false; } + i += 1; + } + ss.len() == 0 +} + +fn test(s: Str) -> bool { + for k in 1..s.len() - 4 { + if testn(&s.clone(), k) { return true; } + } + return false; +} + +fn sp(s: &Str, fx: usize, k: usize) -> Str { + let mut res = Str::new(); + for i in 0..fx { + res.push_back(s[i]); + } + res.push_back(s[k]); + for i in fx..s.len() { + if i != k { res.push_back(s[i]); } + } + res +} + + +fn lp(s: &Str, fx: usize, keep_first: bool, max: u32) -> u32 { + let kf = 0; //if keep_first { 0usize } else { 1usize }; + let mut mymax = max; + for k in (fx + kf)..s.len() { + let ss = sp(&s, fx, k); + if ss != *s || keep_first { + if test(ss.clone()) { + let n = strn(&ss); + if n > mymax { mymax = n } + } + } + let m = lp(&ss, fx + 1, false, mymax); + if m > mymax { mymax = m; } + + } + mymax +} +fn main() { + let mut s = nstr(123456789); + //let mut s = nstr(192384576); + //let mut s = nstr(918273654); + + //let mut hs = HashSet::new(); + let m = lp(&s, 0, true, 0); + println!("{}", m); + + //let n = strn(&s); + //println!("{}", n); + +}