CmpdWords
SRM 268 · 2005-10-18 · by dgoodman
Problem Statement
We have defined a language that consists of a dictionary of distinct words. We want to know whether we should allow the use of compound words constructed by concatenating exactly two distinct dictionary words. The potential problem is that a compound word is ambiguous if either (or both) of the following conditions applies:
- the compound word is a dictionary word
- the compound word can be formed in more than one way.
For example, if "am","eat","a", "meat", "hook", and "meathook" were all in the dictionary, then "meathook" would be ambiguous according to the first condition, and "ameat" would be ambiguous according to the second condition.
Create a class CmpdWords that contains a method ambiguous that is given a
Note that compound words are NOT added to the dictionary. So the dictionary {"a", "b","c"} does not allow "abc" as a compound word.
Constraints
- dictionary will contain between 1 and 50 elements inclusive.
- The elements of dictionary will be distinct.
- Each element of dictionary will contain between 1 and 20 characters inclusive.
- Each character in each element of dictionary will be a lowercase letter 'a'-'z'.
{"am","eat","a", "meat", "hook","meathook"}
Returns: 2
"meathook" and "ameat" are ambiguous as explained above.
{"a","b","c"}
Returns: 0
All the compound words are: "ab","ac","bc","ba","ca","cb" and none of these is ambiguous.
{"a","aa","aaa"}
Returns: 3
The ambiguous words are "aaa": ("a"+"aa" and "aa"+"a" and "aaa") "aaaa": ("a"+"aaa" and "aaa"+"a") "aaaaa": ("aa"+"aaa" and "aaa"+"aa")
{"abc","bca","bab","a"}
Returns: 1
"abc"+"a" = "a"+"bca"
{"ab","ba","aa","aba","a","b"}
Returns: 10
Submissions are judged against all 62 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class CmpdWords with a public method int ambiguous(vector<string> dictionary) · 62 test cases · 2 s / 256 MB per case