You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.1 KiB
107 lines
2.1 KiB
11 months ago
|
use std::collections::HashSet;
|
||
|
use std::collections::VecDeque;
|
||
|
|
||
|
type Str = VecDeque<u8>;
|
||
|
type Res = HashSet<u32>;
|
||
|
|
||
|
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);
|
||
|
|
||
|
}
|