use std::collections::HashMap; use std::io::{Read, Write}; fn words(line: &str) -> Vec { line.split(|c: char| !c.is_ascii_alphabetic()) .filter(|w| !w.is_empty()) .map(|w| w.to_ascii_lowercase().chars().take(16).collect()) .collect() } fn main() { let mut input = String::new(); std::io::stdin().read_to_string(&mut input).unwrap(); let mut lines: Vec<&str> = input.split('\n').collect(); if input.ends_with('\n') { lines.pop(); } let w: usize = lines[0]["workers ".len()..].parse().unwrap(); let text = &lines[1..]; let results: Vec<(usize, usize, HashMap)> = std::thread::scope(|s| { let handles: Vec<_> = (0..w) .map(|i| { s.spawn(move || { let (mut l, mut n, mut m) = (0, 0, HashMap::new()); for line in text.iter().skip(i).step_by(w) { l += 1; for word in words(line) { n += 1; *m.entry(word).or_insert(0) += 1; } } (l, n, m) }) }) .collect(); handles.into_iter().map(|h| h.join().unwrap()).collect() }); let mut out = String::new(); let mut total: HashMap = HashMap::new(); for (i, (l, n, m)) in results.into_iter().enumerate() { out.push_str(&format!("worker {i} lines {l} words {n}\n")); for (k, v) in m { *total.entry(k).or_insert(0) += v; } } out.push_str(&format!("distinct {}\n", total.len())); let mut top: Vec<(String, u64)> = total.into_iter().collect(); top.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.as_bytes().cmp(b.0.as_bytes()))); for (k, v) in top.into_iter().take(10) { out.push_str(&format!("{k} {v}\n")); } std::io::stdout().write_all(out.as_bytes()).unwrap(); }