Connection Status:
Competition Arena > SimilarNames
SRM 599 · 2013-06-25 · by evima · Dynamic Programming, Graph Theory, String Manipulation
Class Name: SimilarNames
Return Type: int
Method Name: count
Arg Types: (vector<string>, vector<int>, vector<int>)
Problem Statement

Problem Statement

Fox Ciel has a list of names on her computer. In this problem, a name is simply a non-empty string of lowercase letters. All names in her list are distinct.

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 String[] names, along with two int[]s info1 and info2. names contains all names in the shuffled list in the order they appear. info1 and info2 describes Ciel's memory of the original list. She remembers that, for each valid i, the info1[i]-th (0-indexed) name in the original list was a prefix of the info2[i]-th name.

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.
  • info1 and info2 will each contain between 0 and 8 elements, inclusive.
  • info1 and info2 will contain the same number of elements.
  • Each element of info1 and info2 will be between 0 and n-1, inclusive, where n is the number of elements in names.
  • For each valid i, info1[i] and info2[i] will be distinct.
  • All pairs (info1[i], info2[i]) will be distinct.
Examples
0)
{"kenta", "kentaro", "ken"}
{0}
{1}
Returns: 3

Here, Ciel's list contains 3 names. She remembers that the 0-th name was a prefix of the 1-th 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.

1)
{"hideo", "hideto", "hideki", "hide"}
{0, 0}
{1, 2}
Returns: 6

She remembers that the 0-th name was a prefix of both the 1-th name and 2-th name in the original list. The only thing we can be sure is that the 0-th name was "hide".

2)
{"aya", "saku", "emi", "ayane", "sakura", "emika", "sakurako"}
{0, 1, 3, 5}
{1, 2, 4, 6}
Returns: 2

This time, she remembers many facts. The only possible original orders are: saku, sakura, sakurako, aya, ayane, emi, emika saku, sakura, sakurako, emi, emika, aya, ayane

3)
{"taro", "jiro", "hanako"}
{0, 1}
{1, 0}
Returns: 0

According to her memory, the 0-th name and the 1-th name in the original list must be the same, but actually all names are distinct. Her memory is inconsistent.

4)
{"alice", "bob", "charlie"}
{}
{}
Returns: 6

Unfortunately she remembers nothing in this case.

Submissions are judged against all 175 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class SimilarNames with a public method int count(vector<string> names, vector<int> info1, vector<int> info2) · 175 test cases · 2 s / 256 MB per case

Submitting as anonymous