Created
July 17, 2023 09:39
-
-
Save starwing/73aef532ca3ad442f6ff70816cdc6064 to your computer and use it in GitHub Desktop.
手机9键双拼方案搜索程序
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{ | |
collections::{HashMap, HashSet}, | |
fmt, | |
fmt::{Display, Formatter}, | |
hash::Hash, | |
iter::FromIterator, | |
sync::OnceLock, | |
}; | |
#[derive(Debug, Hash, Clone, Copy, Eq, PartialEq)] | |
struct Py(i32); | |
#[repr(u8)] | |
#[derive(Debug, Hash, Clone, Copy, Eq, PartialEq)] | |
enum Consonant { | |
Zero, | |
B, | |
P, | |
M, | |
F, | |
D, | |
T, | |
L, | |
N, | |
G, | |
K, | |
H, | |
J, | |
Q, | |
X, | |
Z, | |
C, | |
S, | |
R, | |
Zh, | |
Ch, | |
Sh, | |
W, | |
Y, | |
} | |
#[repr(u8)] | |
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] | |
enum Vowel { | |
Zero, | |
A, | |
O, | |
E, | |
I, | |
U, | |
V, | |
Ai, | |
An, | |
Ang, | |
Ao, | |
Ei, | |
En, | |
Eng, | |
Er, | |
Ia, | |
Iao, | |
Ian, | |
Iang, | |
Ie, | |
Iong, | |
In, | |
Ing, | |
Iu, | |
Ong, | |
Ou, | |
Ua, | |
Uai, | |
Uan, | |
Uang, | |
Ue, | |
Ui, | |
Un, | |
Uo, | |
Ve, | |
} | |
impl Consonant { | |
fn is_zero(&self) -> bool { | |
match self { | |
Consonant::Zero => true, | |
_ => false, | |
} | |
} | |
fn from_str(s: &str) -> Consonant { | |
match s { | |
"b" => Consonant::B, | |
"p" => Consonant::P, | |
"m" => Consonant::M, | |
"f" => Consonant::F, | |
"d" => Consonant::D, | |
"t" => Consonant::T, | |
"l" => Consonant::L, | |
"n" => Consonant::N, | |
"g" => Consonant::G, | |
"k" => Consonant::K, | |
"h" => Consonant::H, | |
"j" => Consonant::J, | |
"q" => Consonant::Q, | |
"x" => Consonant::X, | |
"z" => Consonant::Z, | |
"c" => Consonant::C, | |
"s" => Consonant::S, | |
"r" => Consonant::R, | |
"zh" => Consonant::Zh, | |
"ch" => Consonant::Ch, | |
"sh" => Consonant::Sh, | |
"w" => Consonant::W, | |
"y" => Consonant::Y, | |
_ => Consonant::Zero, | |
} | |
} | |
} | |
impl Display for Consonant { | |
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | |
match *self { | |
Consonant::Zero => write!(f, ""), | |
Consonant::B => write!(f, "b"), | |
Consonant::P => write!(f, "p"), | |
Consonant::M => write!(f, "m"), | |
Consonant::F => write!(f, "f"), | |
Consonant::D => write!(f, "d"), | |
Consonant::T => write!(f, "t"), | |
Consonant::L => write!(f, "l"), | |
Consonant::N => write!(f, "n"), | |
Consonant::G => write!(f, "g"), | |
Consonant::K => write!(f, "k"), | |
Consonant::H => write!(f, "h"), | |
Consonant::J => write!(f, "j"), | |
Consonant::Q => write!(f, "q"), | |
Consonant::X => write!(f, "x"), | |
Consonant::Z => write!(f, "z"), | |
Consonant::C => write!(f, "c"), | |
Consonant::S => write!(f, "s"), | |
Consonant::R => write!(f, "r"), | |
Consonant::Zh => write!(f, "zh"), | |
Consonant::Ch => write!(f, "ch"), | |
Consonant::Sh => write!(f, "sh"), | |
Consonant::W => write!(f, "w"), | |
Consonant::Y => write!(f, "y"), | |
} | |
} | |
} | |
impl Vowel { | |
fn is_zero(&self) -> bool { | |
match self { | |
Vowel::Zero => true, | |
_ => false, | |
} | |
} | |
fn from_str(s: &str) -> Vowel { | |
match s { | |
"a" => Vowel::A, | |
"o" => Vowel::O, | |
"e" => Vowel::E, | |
"i" => Vowel::I, | |
"u" => Vowel::U, | |
"v" => Vowel::V, | |
"ai" => Vowel::Ai, | |
"an" => Vowel::An, | |
"ang" => Vowel::Ang, | |
"ao" => Vowel::Ao, | |
"ei" => Vowel::Ei, | |
"en" => Vowel::En, | |
"eng" => Vowel::Eng, | |
"er" => Vowel::Er, | |
"ia" => Vowel::Ia, | |
"iao" => Vowel::Iao, | |
"ian" => Vowel::Ian, | |
"iang" => Vowel::Iang, | |
"ie" => Vowel::Ie, | |
"iong" => Vowel::Iong, | |
"in" => Vowel::In, | |
"ing" => Vowel::Ing, | |
"iu" => Vowel::Iu, | |
"ong" => Vowel::Ong, | |
"ou" => Vowel::Ou, | |
"ua" => Vowel::Ua, | |
"uai" => Vowel::Uai, | |
"uan" => Vowel::Uan, | |
"uang" => Vowel::Uang, | |
"ue" => Vowel::Ue, | |
"ui" => Vowel::Ui, | |
"un" => Vowel::Un, | |
"uo" => Vowel::Uo, | |
"ve" => Vowel::Ve, | |
_ => Vowel::Zero, | |
} | |
} | |
} | |
impl Display for Vowel { | |
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | |
match *self { | |
Vowel::Zero => write!(f, ""), | |
Vowel::A => write!(f, "a"), | |
Vowel::O => write!(f, "o"), | |
Vowel::E => write!(f, "e"), | |
Vowel::I => write!(f, "i"), | |
Vowel::U => write!(f, "u"), | |
Vowel::V => write!(f, "v"), | |
Vowel::Ai => write!(f, "ai"), | |
Vowel::An => write!(f, "an"), | |
Vowel::Ang => write!(f, "ang"), | |
Vowel::Ao => write!(f, "ao"), | |
Vowel::Ei => write!(f, "ei"), | |
Vowel::En => write!(f, "en"), | |
Vowel::Eng => write!(f, "eng"), | |
Vowel::Er => write!(f, "er"), | |
Vowel::Ia => write!(f, "ia"), | |
Vowel::Iao => write!(f, "iao"), | |
Vowel::Ian => write!(f, "ian"), | |
Vowel::Iang => write!(f, "iang"), | |
Vowel::Ie => write!(f, "ie"), | |
Vowel::Iong => write!(f, "iong"), | |
Vowel::In => write!(f, "in"), | |
Vowel::Ing => write!(f, "ing"), | |
Vowel::Iu => write!(f, "iu"), | |
Vowel::Ong => write!(f, "ong"), | |
Vowel::Ou => write!(f, "ou"), | |
Vowel::Ua => write!(f, "ua"), | |
Vowel::Uai => write!(f, "uai"), | |
Vowel::Uan => write!(f, "uan"), | |
Vowel::Uang => write!(f, "uang"), | |
Vowel::Ue => write!(f, "ue"), | |
Vowel::Ui => write!(f, "ui"), | |
Vowel::Un => write!(f, "un"), | |
Vowel::Uo => write!(f, "uo"), | |
Vowel::Ve => write!(f, "ve"), | |
} | |
} | |
} | |
impl Display for Py { | |
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | |
self.consonant().fmt(f) | |
.and_then(|_| self.vowel().fmt(f)) | |
} | |
} | |
static VALID_PY: OnceLock<HashSet<Py>> = OnceLock::new(); | |
impl Py { | |
fn from_str(s: &str) -> Py { | |
let v = Vowel::from_str(s); | |
if !v.is_zero() { | |
return Py::new_vowel(v) | |
} | |
if s.len() >= 2 { | |
let c = Consonant::from_str(&s[..2]); | |
let v = Vowel::from_str(&s[2..]); | |
if !c.is_zero() && !v.is_zero() { | |
return Py::new(c, v) | |
} | |
} | |
let c = Consonant::from_str(&s[..1]); | |
let v = Vowel::from_str(&s[1..]); | |
if !c.is_zero() && !v.is_zero() { | |
return Py::new(c, v) | |
} | |
panic!("invalid pinyin: {}", s) | |
} | |
fn new(c: Consonant, v: Vowel) -> Self { | |
Py(((c as i32) << 8) | (v as i32)) | |
} | |
fn new_vowel(v: Vowel) -> Self { | |
Py(v as i32) | |
} | |
fn consonant(&self) -> Consonant { | |
unsafe { std::mem::transmute((self.0 >> 8) as u8) } | |
} | |
fn vowel(&self) -> Vowel { | |
unsafe { std::mem::transmute((self.0 & 0xff) as u8) } | |
} | |
} | |
#[derive(Debug, Default)] | |
struct Solution { | |
consonants: [Vec<Consonant>; 9], | |
vowels: [Vec<Vowel>; 9], | |
} | |
impl Display for Solution { | |
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | |
for i in 0..9 { | |
write!(f, "{}: [", i + 1)?; | |
for c in &self.consonants[i] { | |
write!(f, "{} ", c)?; | |
} | |
write!(f, "] [")?; | |
for v in &self.vowels[i] { | |
write!( | |
f, | |
"{}{} ", | |
v, | |
if XH9_MAP.get().unwrap().vmap[v] != i as i32 + 1 { | |
"*" | |
} else { | |
"" | |
} | |
)? | |
} | |
writeln!(f, "]")?; | |
} | |
Ok(()) | |
} | |
} | |
static XH9: OnceLock<Solution> = OnceLock::new(); | |
#[derive(Debug, Default, Clone)] | |
struct PyMap { | |
cmap: HashMap<Consonant, i32>, | |
vmap: HashMap<Vowel, i32>, | |
} | |
static XH9_MAP: OnceLock<PyMap> = OnceLock::new(); | |
impl PyMap { | |
fn make_map(s: &Solution) -> PyMap { | |
let mut cmap = HashMap::new(); | |
let mut vmap = HashMap::new(); | |
s.consonants.iter().enumerate().for_each(|(i, cs)| { | |
for c in cs { | |
cmap.insert(*c, i as i32 + 1); | |
} | |
}); | |
s.vowels.iter().enumerate().for_each(|(i, vs)| { | |
for v in vs { | |
vmap.insert(*v, i as i32 + 1); | |
} | |
}); | |
PyMap { cmap, vmap } | |
} | |
fn to_solution(&self) -> Solution { | |
let mut s = Solution::default(); | |
for (c, i) in self.cmap.iter() { | |
s.consonants[*i as usize - 1].push(*c); | |
} | |
for (v, i) in self.vmap.iter() { | |
s.vowels[*i as usize - 1].push(*v); | |
} | |
s | |
} | |
fn calc_result(&self) -> MapResult { | |
let mut m = MapResult::default(); | |
for py in VALID_PY.get().unwrap().iter() { | |
let id = self.cmap[&py.consonant()] * 10 + self.vmap[&py.vowel()]; | |
m.0.entry(id).or_default().push(*py) | |
} | |
m | |
} | |
} | |
#[derive(Debug, Default)] | |
struct MapResult(HashMap<i32, Vec<Py>>); | |
impl MapResult { | |
fn calc_maxlength(&self) -> (usize, usize) { | |
let mut max = 0; | |
let mut maxcnt = 0; | |
for (_, v) in self.0.iter() { | |
if v.len() > max { | |
max = v.len(); | |
maxcnt = 0; | |
} | |
if v.len() == max { | |
maxcnt += 1 | |
} | |
} | |
(max, maxcnt) | |
} | |
} | |
#[derive(Debug, Default)] | |
struct SearchContext { | |
pymap: PyMap, | |
max: (usize, usize, usize), | |
moved: HashSet<Vowel>, | |
best_solution: Vec<Solution>, | |
} | |
impl SearchContext { | |
fn new(init: PyMap) -> SearchContext { | |
SearchContext { | |
pymap: init, | |
max: (usize::MAX, usize::MAX, usize::MAX), | |
..Default::default() | |
} | |
} | |
fn pivot(&mut self) -> Vec<(Vowel, i32)> { | |
let mut result = Vec::new(); | |
let vs = self.pymap.vmap.clone().into_iter().collect::<Vec<_>>(); | |
for (v, n) in vs { | |
if self.moved.contains(&v) { | |
continue | |
} | |
for i in 1..10 { | |
if i == n { | |
continue | |
} | |
self.pymap.vmap.insert(v, i); | |
let cr = self.pymap.calc_result(); | |
let length = cr.calc_maxlength(); | |
let cur = (length.0, self.moved.len()+1, length.1); | |
if cur < self.max { | |
println!("found solutions for ({}, {}) with move {}", length.0, length.1, self.moved.len()+1); | |
self.max = cur; | |
self.best_solution.clear(); | |
} | |
if self.max.0 == length.0 { | |
result.push((v, i)); | |
self.best_solution.push(self.pymap.to_solution()) | |
} | |
if length.0 <= 7 { | |
println!( | |
"find new solution for length={},{} with move {}: \n{}\n******************", | |
length.0, | |
length.1, | |
self.moved.len()+1, | |
self.pymap.to_solution() | |
); | |
} | |
self.pymap.vmap.insert(v, n); | |
} | |
} | |
result | |
} | |
fn search(&mut self, lv: usize) { | |
if lv == 0 { | |
return; | |
} | |
for (v, n) in self.pivot() { | |
let oldn = self.pymap.vmap[&v]; | |
self.pymap.vmap.insert(v, n); | |
self.moved.insert(v); | |
self.search(lv - 1); | |
self.pymap.vmap.insert(v, oldn); | |
self.moved.remove(&v); | |
} | |
} | |
} | |
fn main() { | |
VALID_PY.get_or_init(|| { | |
HashSet::from_iter([ | |
Py::from_str("a"), | |
Py::from_str("ai"), | |
Py::from_str("an"), | |
Py::from_str("ang"), | |
Py::from_str("ao"), | |
Py::from_str("ba"), | |
Py::from_str("bai"), | |
Py::from_str("ban"), | |
Py::from_str("bang"), | |
Py::from_str("bao"), | |
Py::from_str("bei"), | |
Py::from_str("ben"), | |
Py::from_str("beng"), | |
Py::from_str("bi"), | |
Py::from_str("bian"), | |
Py::from_str("biao"), | |
Py::from_str("bie"), | |
Py::from_str("bin"), | |
Py::from_str("bing"), | |
Py::from_str("bo"), | |
Py::from_str("bu"), | |
Py::from_str("ca"), | |
Py::from_str("cai"), | |
Py::from_str("can"), | |
Py::from_str("cang"), | |
Py::from_str("cao"), | |
Py::from_str("ce"), | |
Py::from_str("cei"), | |
Py::from_str("cen"), | |
Py::from_str("ceng"), | |
Py::from_str("ci"), | |
Py::from_str("cong"), | |
Py::from_str("cou"), | |
Py::from_str("cu"), | |
Py::from_str("cuan"), | |
Py::from_str("cui"), | |
Py::from_str("cun"), | |
Py::from_str("cuo"), | |
Py::from_str("cha"), | |
Py::from_str("chai"), | |
Py::from_str("chan"), | |
Py::from_str("chang"), | |
Py::from_str("chao"), | |
Py::from_str("che"), | |
Py::from_str("chen"), | |
Py::from_str("cheng"), | |
Py::from_str("chi"), | |
Py::from_str("chong"), | |
Py::from_str("chou"), | |
Py::from_str("chu"), | |
Py::from_str("chua"), | |
Py::from_str("chuai"), | |
Py::from_str("chuan"), | |
Py::from_str("chuang"), | |
Py::from_str("chui"), | |
Py::from_str("chun"), | |
Py::from_str("chuo"), | |
Py::from_str("da"), | |
Py::from_str("dai"), | |
Py::from_str("dan"), | |
Py::from_str("dang"), | |
Py::from_str("dao"), | |
Py::from_str("de"), | |
Py::from_str("dei"), | |
Py::from_str("den"), | |
Py::from_str("deng"), | |
Py::from_str("di"), | |
Py::from_str("dia"), | |
Py::from_str("dian"), | |
Py::from_str("diao"), | |
Py::from_str("die"), | |
Py::from_str("ding"), | |
Py::from_str("diu"), | |
Py::from_str("dong"), | |
Py::from_str("dou"), | |
Py::from_str("du"), | |
Py::from_str("duan"), | |
Py::from_str("dui"), | |
Py::from_str("dun"), | |
Py::from_str("duo"), | |
Py::from_str("e"), | |
Py::from_str("ei"), | |
Py::from_str("en"), | |
Py::from_str("eng"), | |
Py::from_str("er"), | |
Py::from_str("fa"), | |
Py::from_str("fan"), | |
Py::from_str("fang"), | |
Py::from_str("fei"), | |
Py::from_str("fen"), | |
Py::from_str("feng"), | |
Py::from_str("fo"), | |
Py::from_str("fou"), | |
Py::from_str("fu"), | |
Py::from_str("ga"), | |
Py::from_str("gai"), | |
Py::from_str("gan"), | |
Py::from_str("gang"), | |
Py::from_str("gao"), | |
Py::from_str("ge"), | |
Py::from_str("gei"), | |
Py::from_str("gen"), | |
Py::from_str("geng"), | |
Py::from_str("gong"), | |
Py::from_str("gou"), | |
Py::from_str("gu"), | |
Py::from_str("gua"), | |
Py::from_str("guai"), | |
Py::from_str("guan"), | |
Py::from_str("guang"), | |
Py::from_str("gui"), | |
Py::from_str("gun"), | |
Py::from_str("guo"), | |
Py::from_str("ha"), | |
Py::from_str("hai"), | |
Py::from_str("han"), | |
Py::from_str("hang"), | |
Py::from_str("hao"), | |
Py::from_str("he"), | |
Py::from_str("hei"), | |
Py::from_str("hen"), | |
Py::from_str("heng"), | |
Py::from_str("hong"), | |
Py::from_str("hou"), | |
Py::from_str("hu"), | |
Py::from_str("hua"), | |
Py::from_str("huai"), | |
Py::from_str("huan"), | |
Py::from_str("huang"), | |
Py::from_str("hui"), | |
Py::from_str("hun"), | |
Py::from_str("huo"), | |
Py::from_str("ji"), | |
Py::from_str("jia"), | |
Py::from_str("jian"), | |
Py::from_str("jiang"), | |
Py::from_str("jiao"), | |
Py::from_str("jie"), | |
Py::from_str("jin"), | |
Py::from_str("jing"), | |
Py::from_str("jiong"), | |
Py::from_str("jiu"), | |
Py::from_str("ju"), | |
Py::from_str("juan"), | |
Py::from_str("jue"), | |
Py::from_str("jun"), | |
Py::from_str("ka"), | |
Py::from_str("kai"), | |
Py::from_str("kan"), | |
Py::from_str("kang"), | |
Py::from_str("kao"), | |
Py::from_str("ke"), | |
Py::from_str("ken"), | |
Py::from_str("keng"), | |
Py::from_str("kong"), | |
Py::from_str("kou"), | |
Py::from_str("ku"), | |
Py::from_str("kua"), | |
Py::from_str("kuai"), | |
Py::from_str("kuan"), | |
Py::from_str("kuang"), | |
Py::from_str("kui"), | |
Py::from_str("kun"), | |
Py::from_str("kuo"), | |
Py::from_str("la"), | |
Py::from_str("lai"), | |
Py::from_str("lan"), | |
Py::from_str("lang"), | |
Py::from_str("lao"), | |
Py::from_str("le"), | |
Py::from_str("lei"), | |
Py::from_str("leng"), | |
Py::from_str("li"), | |
Py::from_str("lia"), | |
Py::from_str("lian"), | |
Py::from_str("liang"), | |
Py::from_str("liao"), | |
Py::from_str("lie"), | |
Py::from_str("lin"), | |
Py::from_str("ling"), | |
Py::from_str("liu"), | |
Py::from_str("long"), | |
Py::from_str("lou"), | |
Py::from_str("lu"), | |
Py::from_str("luan"), | |
Py::from_str("lun"), | |
Py::from_str("luo"), | |
Py::from_str("lv"), | |
Py::from_str("lve"), | |
// Py::from_str("m"), | |
Py::from_str("ma"), | |
Py::from_str("mai"), | |
Py::from_str("man"), | |
Py::from_str("mang"), | |
Py::from_str("mao"), | |
Py::from_str("me"), | |
Py::from_str("mei"), | |
Py::from_str("men"), | |
Py::from_str("meng"), | |
Py::from_str("mi"), | |
Py::from_str("mian"), | |
Py::from_str("miao"), | |
Py::from_str("mie"), | |
Py::from_str("min"), | |
Py::from_str("ming"), | |
Py::from_str("miu"), | |
Py::from_str("mo"), | |
Py::from_str("mou"), | |
Py::from_str("mu"), | |
Py::from_str("na"), | |
Py::from_str("nai"), | |
Py::from_str("nan"), | |
Py::from_str("nang"), | |
Py::from_str("nao"), | |
Py::from_str("ne"), | |
Py::from_str("nei"), | |
Py::from_str("nen"), | |
Py::from_str("neng"), | |
// Py::from_str("ng"), | |
Py::from_str("ni"), | |
Py::from_str("nian"), | |
Py::from_str("niang"), | |
Py::from_str("niao"), | |
Py::from_str("nie"), | |
Py::from_str("nin"), | |
Py::from_str("ning"), | |
Py::from_str("niu"), | |
Py::from_str("nong"), | |
Py::from_str("nou"), | |
Py::from_str("nu"), | |
Py::from_str("nuan"), | |
Py::from_str("nun"), | |
Py::from_str("nuo"), | |
Py::from_str("nv"), | |
Py::from_str("nve"), | |
Py::from_str("o"), | |
Py::from_str("ou"), | |
Py::from_str("pa"), | |
Py::from_str("pai"), | |
Py::from_str("pan"), | |
Py::from_str("pang"), | |
Py::from_str("pao"), | |
Py::from_str("pei"), | |
Py::from_str("pen"), | |
Py::from_str("peng"), | |
Py::from_str("pi"), | |
Py::from_str("pian"), | |
Py::from_str("piao"), | |
Py::from_str("pie"), | |
Py::from_str("pin"), | |
Py::from_str("ping"), | |
Py::from_str("po"), | |
Py::from_str("pou"), | |
Py::from_str("pu"), | |
Py::from_str("qi"), | |
Py::from_str("qia"), | |
Py::from_str("qian"), | |
Py::from_str("qiang"), | |
Py::from_str("qiao"), | |
Py::from_str("qie"), | |
Py::from_str("qin"), | |
Py::from_str("qing"), | |
Py::from_str("qiong"), | |
Py::from_str("qiu"), | |
Py::from_str("qu"), | |
Py::from_str("quan"), | |
Py::from_str("que"), | |
Py::from_str("qun"), | |
Py::from_str("ran"), | |
Py::from_str("rang"), | |
Py::from_str("rao"), | |
Py::from_str("re"), | |
Py::from_str("ren"), | |
Py::from_str("reng"), | |
Py::from_str("ri"), | |
Py::from_str("rong"), | |
Py::from_str("rou"), | |
Py::from_str("ru"), | |
Py::from_str("ruan"), | |
Py::from_str("rui"), | |
Py::from_str("run"), | |
Py::from_str("ruo"), | |
Py::from_str("sa"), | |
Py::from_str("sai"), | |
Py::from_str("san"), | |
Py::from_str("sang"), | |
Py::from_str("sao"), | |
Py::from_str("se"), | |
Py::from_str("sen"), | |
Py::from_str("seng"), | |
Py::from_str("si"), | |
Py::from_str("song"), | |
Py::from_str("sou"), | |
Py::from_str("su"), | |
Py::from_str("suan"), | |
Py::from_str("sui"), | |
Py::from_str("sun"), | |
Py::from_str("suo"), | |
Py::from_str("sha"), | |
Py::from_str("shai"), | |
Py::from_str("shan"), | |
Py::from_str("shang"), | |
Py::from_str("shao"), | |
Py::from_str("she"), | |
Py::from_str("shei"), | |
Py::from_str("shen"), | |
Py::from_str("sheng"), | |
Py::from_str("shi"), | |
Py::from_str("shou"), | |
Py::from_str("shu"), | |
Py::from_str("shua"), | |
Py::from_str("shuai"), | |
Py::from_str("shuan"), | |
Py::from_str("shuang"), | |
Py::from_str("shui"), | |
Py::from_str("shun"), | |
Py::from_str("shuo"), | |
Py::from_str("ta"), | |
Py::from_str("tai"), | |
Py::from_str("tan"), | |
Py::from_str("tang"), | |
Py::from_str("tao"), | |
Py::from_str("te"), | |
Py::from_str("teng"), | |
Py::from_str("ti"), | |
Py::from_str("tian"), | |
Py::from_str("tiao"), | |
Py::from_str("tie"), | |
Py::from_str("ting"), | |
Py::from_str("tong"), | |
Py::from_str("tou"), | |
Py::from_str("tu"), | |
Py::from_str("tuan"), | |
Py::from_str("tui"), | |
Py::from_str("tun"), | |
Py::from_str("tuo"), | |
Py::from_str("wa"), | |
Py::from_str("wai"), | |
Py::from_str("wan"), | |
Py::from_str("wang"), | |
Py::from_str("wei"), | |
Py::from_str("wen"), | |
Py::from_str("weng"), | |
Py::from_str("wo"), | |
Py::from_str("wu"), | |
Py::from_str("xi"), | |
Py::from_str("xia"), | |
Py::from_str("xian"), | |
Py::from_str("xiang"), | |
Py::from_str("xiao"), | |
Py::from_str("xie"), | |
Py::from_str("xin"), | |
Py::from_str("xing"), | |
Py::from_str("xiong"), | |
Py::from_str("xiu"), | |
Py::from_str("xu"), | |
Py::from_str("xuan"), | |
Py::from_str("xue"), | |
Py::from_str("xun"), | |
Py::from_str("ya"), | |
Py::from_str("yan"), | |
Py::from_str("yang"), | |
Py::from_str("yao"), | |
Py::from_str("ye"), | |
Py::from_str("yi"), | |
Py::from_str("yin"), | |
Py::from_str("ying"), | |
Py::from_str("yo"), | |
Py::from_str("yong"), | |
Py::from_str("you"), | |
Py::from_str("yu"), | |
Py::from_str("yuan"), | |
Py::from_str("yue"), | |
Py::from_str("yun"), | |
Py::from_str("za"), | |
Py::from_str("zai"), | |
Py::from_str("zan"), | |
Py::from_str("zang"), | |
Py::from_str("zao"), | |
Py::from_str("ze"), | |
Py::from_str("zei"), | |
Py::from_str("zen"), | |
Py::from_str("zeng"), | |
Py::from_str("zi"), | |
Py::from_str("zong"), | |
Py::from_str("zou"), | |
Py::from_str("zu"), | |
Py::from_str("zuan"), | |
Py::from_str("zui"), | |
Py::from_str("zun"), | |
Py::from_str("zuo"), | |
Py::from_str("zha"), | |
Py::from_str("zhai"), | |
Py::from_str("zhan"), | |
Py::from_str("zhang"), | |
Py::from_str("zhao"), | |
Py::from_str("zhe"), | |
Py::from_str("zhei"), | |
Py::from_str("zhen"), | |
Py::from_str("zheng"), | |
Py::from_str("zhi"), | |
Py::from_str("zhong"), | |
Py::from_str("zhou"), | |
Py::from_str("zhu"), | |
Py::from_str("zhua"), | |
Py::from_str("zhuai"), | |
Py::from_str("zhuan"), | |
Py::from_str("zhuang"), | |
Py::from_str("zhui"), | |
Py::from_str("zhun"), | |
Py::from_str("zhuo"), | |
]) | |
}); | |
XH9.get_or_init(|| Solution { | |
consonants: [ | |
vec![Consonant::X, Consonant::R, Consonant::Zero], | |
vec![Consonant::B, Consonant::C, Consonant::Sh], | |
vec![Consonant::D, Consonant::F, Consonant::Ch], | |
vec![Consonant::G, Consonant::H], | |
vec![Consonant::J, Consonant::K, Consonant::L], | |
vec![Consonant::M, Consonant::N], | |
vec![Consonant::P, Consonant::Q, Consonant::S], | |
vec![Consonant::T, Consonant::Zh], | |
vec![Consonant::W, Consonant::Y, Consonant::Z], | |
], | |
vowels: [ | |
vec![Vowel::Ia, Vowel::Ua, Vowel::Uan], | |
vec![Vowel::A, Vowel::In, Vowel::Ao], | |
vec![Vowel::E, Vowel::Ai, Vowel::En, Vowel::Er], | |
vec![Vowel::I, Vowel::Eng, Vowel::Ang], | |
vec![Vowel::An, Vowel::Ing, Vowel::Uang, Vowel::Iang, Vowel::Uai], | |
vec![Vowel::O, Vowel::Uo, Vowel::Ian, Vowel::Iao], | |
vec![Vowel::Ie, Vowel::Iu, Vowel::Ong, Vowel::Iong], | |
vec![Vowel::U, Vowel::V, Vowel::Ui, Vowel::Ue, Vowel::Ve], | |
vec![Vowel::Ei, Vowel::Un, Vowel::Ou], | |
], | |
}); | |
XH9_MAP.get_or_init(|| PyMap::make_map(XH9.get().unwrap())); | |
let mut sc = SearchContext::new(XH9_MAP.get().unwrap().clone()); | |
sc.search(3); | |
for s in &sc.best_solution { | |
let pymap = PyMap::make_map(s); | |
let result = pymap.calc_result(); | |
let length = result.calc_maxlength(); | |
println!("found solution with length ({},{}):\n{}", length.0, length.1, s) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment