File size: 3,915 Bytes
b27bfb3 | 1 2 3 4 5 | {"type": "session", "harness": "cp-practice-sim", "id": "cp-0034", "name": "Scrambled Word Families", "mode": "chat", "topic": "Hashing and counting (maps/sets)", "difficulty": "easy", "tags": ["hashing", "strings", "anagrams"], "explanation_mode": "concise-rationale", "tool_scenario": "none", "expected_recovery": "N/A - no fault injected.", "languages": ["rust", "typescript", "cpp"], "unexecuted_languages": [], "time_complexity": "O(L log L) for total length L", "space_complexity": "O(L)"}
{"type": "message", "message": {"role": "system", "content": "You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space complexity. ", "timestamp": 1794122401700}}
{"type": "message", "message": {"role": "user", "content": "# Scrambled Word Families\n\nTopic: Hashing and counting (maps/sets) | Difficulty: easy\n\n## Problem\nTwo words belong to the same family if one can be rearranged into the other (they are anagrams: same letters with same multiplicities). Given n words, find how many different families occur and the size of the largest family.\n\n## Input\nThe first line contains n. Each of the next n lines contains one word of lowercase letters.\n\n## Output\nPrint two integers separated by a space: the number of families and the size of the largest family.\n\n## Constraints\n- 1 <= n <= 10^5\n- Each word has length at least 1; the total length of all words is at most 2*10^5\n\n## Examples\n### Example 1\nInput:\n```\n6\nlisten\nsilent\nenlist\ngoogle\ngogole\ncat\n```\nOutput:\n```\n3 3\n```\n\n### Example 2\nInput:\n```\n3\nab\nab\nba\n```\nOutput:\n```\n1 3\n```\nNote: Identical words count separately and belong to the same family.\n\nSolve this in Rust, TypeScript, C++17, one complete program per language. Include a concise rationale (approach, correctness idea, time/space complexity).", "timestamp": 1794122403537}}
{"type": "message", "message": {"role": "assistant", "content": "**Approach.** Sort the letters of every word to get a canonical key and count keys in a hash map. The map size is the number of families and the maximum value is the largest family.\n\n**Correctness.** Two words are anagrams exactly when their sorted letter sequences are equal, so keys partition the words into families.\n\n**Complexity.** Time: O(L log L) for total length L. Space: O(L).\n\n### Rust\n```rust\nuse std::collections::HashMap;\nuse std::io::*;\nfn main() {\n let mut t = String::new();\n stdin().read_to_string(&mut t).unwrap();\n let mut it = t.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut m: HashMap<Vec<u8>, usize> = HashMap::new();\n let mut best = 0;\n for _ in 0..n {\n let mut w = it.next().unwrap().as_bytes().to_vec();\n w.sort();\n let c = m.entry(w).or_insert(0);\n *c += 1;\n best = best.max(*c);\n }\n println!(\"{} {}\", m.len(), best);\n}\n```\n\n### TypeScript\n```typescript\ndeclare const require: any;\nconst tok: string[] = require('fs').readFileSync(0, 'utf8').split(/\\s+/).filter((s: string) => s.length > 0);\nconst n: number = parseInt(tok[0]);\nconst m = new Map<string, number>();\nlet best = 0;\nfor (let i = 1; i <= n; i++) {\n const key = tok[i].split('').sort().join('');\n const c = (m.get(key) || 0) + 1;\n m.set(key, c);\n if (c > best) best = c;\n}\nconsole.log(m.size + \" \" + best);\n```\n\n### C++17\n```cpp\n#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n;\n cin >> n;\n unordered_map<string, int> m;\n int best = 0;\n for (int i = 0; i < n; i++) {\n string s;\n cin >> s;\n sort(s.begin(), s.end());\n best = max(best, ++m[s]);\n }\n cout << m.size() << \" \" << best << endl;\n}\n```", "timestamp": 1794122405511}}
|