back to the menu

Flag Counter TopCoder SRM 599 - 950: SimilarNames

SRM 599 1 950 SimilarNames


Problem Statement

输入n个字符串,现在需要给它们从0到n - 1标号,
满足m条形如 标号为a[i]的字符串是标号为b[i]的字符串的前缀 的限制。
求标号方案数,模10^9+7输出。
n,字符串长度 <= 50, m <= 8。

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.

Definition

(be sure your method is public)

Limits

Notes

Constraints

Test cases

  1. input
    • names { "kenta", "kentaro", "ken" }
    • info1 { 0 }
    • info2 { 1 }
    output
    Returns 3
    note
    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.
  2. input
    • names { "hideo", "hideto", "hideki", "hide" }
    • info1 { 0, 0 }
    • info2 { 1, 2 }
    output
    Returns 6
    note
    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".
  3. input
    • names { "aya", "saku", "emi", "ayane", "sakura", "emika", "sakurako" }
    • info1 { 0, 1, 3, 5 }
    • info2 { 1, 2, 4, 6 }
    output
    Returns 2
    note
    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
  4. input
    • names { "taro", "jiro", "hanako" }
    • info1 { 0, 1 }
    • info2 { 1, 0 }
    output
    Returns 0
    note
    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.
  5. input
    • names { "alice", "bob", "charlie" }
    • info1 { }
    • info2 { }
    output
    Returns 6
    note
    Unfortunately she remembers nothing in this case.
  6. input
    • names { "ryota", "ryohei", "ryotaro", "ryo", "ryoga", "ryoma", "ryoko", "ryosuke", "ciel", "lun", "ryuta", "ryuji", "ryuma", "ryujiro", "ryusuke", "ryutaro", "ryu", "ryuhei", "ryuichi", "evima" }
    • info1 { 17, 5, 6, 13, 5 }
    • info2 { 9, 2, 14, 17, 14 }
    output
    Returns 994456648

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.