RosettaCodeData/Task/ABC-Problem/Rust/abc-problem.rust

30 lines
1.1 KiB
Text
Raw Permalink Normal View History

2015-11-18 06:14:39 +00:00
use std::iter::repeat;
2015-02-20 09:02:09 -05:00
fn rec_can_make_word(index: usize, word: &str, blocks: &[&str], used: &mut[bool]) -> bool {
2015-11-18 06:14:39 +00:00
let c = word.chars().nth(index).unwrap().to_uppercase().next().unwrap();
for i in 0..blocks.len() {
if !used[i] && blocks[i].chars().any(|s| s == c) {
used[i] = true;
if index == 0 || rec_can_make_word(index - 1, word, blocks, used) {
return true;
}
used[i] = false;
}
}
false
2015-02-20 09:02:09 -05:00
}
2015-11-18 06:14:39 +00:00
2015-02-20 09:02:09 -05:00
fn can_make_word(word: &str, blocks: &[&str]) -> bool {
2015-11-18 06:14:39 +00:00
return rec_can_make_word(word.chars().count() - 1, word, blocks,
&mut repeat(false).take(blocks.len()).collect::<Vec<_>>());
2015-02-20 09:02:09 -05:00
}
fn main() {
2015-11-18 06:14:39 +00:00
let blocks = [("BO"), ("XK"), ("DQ"), ("CP"), ("NA"), ("GT"), ("RE"), ("TG"), ("QD"), ("FS"),
("JW"), ("HU"), ("VI"), ("AN"), ("OB"), ("ER"), ("FS"), ("LY"), ("PC"), ("ZM")];
let words = ["A", "BARK", "BOOK", "TREAT", "COMMON", "SQUAD", "CONFUSE"];
for word in &words {
println!("{} -> {}", word, can_make_word(word, &blocks))
}
2015-02-20 09:02:09 -05:00
}