SentenceDecomposition
SRM 411 · 2008-07-24 · by hken
Problem Statement
All the valid words that can be used in the new language are given in the
Although several different sequences of valid words can produce the same sentence in this language, only the sequence with the least total transformation cost carries the meaning of the sentence. The advantage of the new language is that the parents can no longer understand what the kids are saying. The disadvantage is that the kids themselves also do not understand. They need your help.
Given a
Notes
- If a word is used multiple times in a sentence, each occurrence can be transformed differently.
Constraints
- sentence will contain between 1 and 50 lowercase letters ('a'-'z'), inclusive.
- validWords will contain between 1 and 50 elements, inclusive.
- Each element of validWords will contain between 1 and 50 lowercase letters ('a'-'z'), inclusive.
"neotowheret"
{"one", "two", "three", "there"}
Returns: 8
The following transformations can be made: "one" -> "neo" with a cost of 3 "two" -> "tow" with a cost of 2 "three" -> "heret" with a cost of 3 "there" -> "heret" with a cost of 5 So the sequence {"one", "two", "three"} is the one carrying the meaning of "neotowheret". Its total transformation cost is 3 + 2 + 3 = 8.
"abba"
{"ab", "ac", "ad"}
Returns: 2
The word "ab" is used twice, and each time, it is transformed differently.
"thisismeaningless"
{"this", "is", "meaningful"}
Returns: -1
"ommwreehisymkiml"
{"we", "were", "here", "my", "is", "mom", "here", "si", "milk", "where", "si"}
Returns: 10
"aaaaa"
{"aa", "aaa", "aaa"}
Returns: 0
"sepawaterords"
{"separate","words"}
Returns: -1
You are only allowed to rearrange letters within words, and not in the entire sentence.
Submissions are judged against all 232 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class SentenceDecomposition with a public method int decompose(string sentence, vector<string> validWords) · 232 test cases · 2 s / 256 MB per case