SimilarNames2
SRM 599 · 2013-06-25 · by evima
Problem Statement
One day, when she left her seat, she forgot to lock her computer. Then, Lun the mischievous dog appeared, and randomly shuffled the order of the names in her list.
Now, Ciel has to restore the original order of names using her memory. You are given a
Let X be the number of possible orders of the names in the original list that are consistent with Ciel's memory. Calculate and return the value (X modulo 1,000,000,007). X can be 0, which means Ciel's memory is inconsistent with the names in the list.
Notes
- A prefix of a string is the result of erasing zero or more characters from the right end of that string.
Constraints
- names will contain between 2 and 50 elements, inclusive.
- Each element of names will contain between 1 and 50 characters, inclusive.
- Each character of each element of names will be a lowercase letter ('a'-'z').
- Elements of names will be distinct.
- L will be between 1 and n, inclusive, where n is the number of elements in names.
{"kenta", "kentaro", "ken"}
2
Returns: 3
Here, Ciel's list contains 3 names. She remembers that the 0-th name was a prefix of the 1-st name in the original list. Here are the all possible orders of names in the original list: ken, kenta, kentaro ken, kentaro, kenta kenta, kentaro, ken Note that it is possible that the order of the names in the original list coincides with that of the shuffled list.
{"hideo", "hideto", "hideki", "hide"}
2
Returns: 6
Again, she remembers that the 0-th name was a prefix of the 1-st name in the original list. The only thing we can be sure is that the 0-th name was "hide".
{"aya", "saku", "emi", "ayane", "sakura", "emika", "sakurako"}
3
Returns: 24
This time, she remembers not only the fact that the 0-th name was a prefix of the 1-st name, but also the fact that the 1-st name was a prefix of the 2-nd name. The first 3 names should be "saku", "sakura", "sakurako" in this order.
{"taro", "jiro", "hanako"}
2
Returns: 0
No name is a prefix of another name in this case, so her memory is inconsistent.
{"alice", "bob", "charlie"}
1
Returns: 6
L = 1 means that she actually remembers nothing.
Submissions are judged against all 130 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class SimilarNames2 with a public method int count(vector<string> names, int L) · 130 test cases · 2 s / 256 MB per case