TopcoderArchiveVisit Topcoder
SRM3 Dec 2009
Member SRM 471

EllysPlaylists

Problem statement, definition, constraints, and public examples.

Problem Statement

Elly is great fan of consistency. She would like to have order in even the simplest things in life – like listening to music.

She has chosen several songs from which she wants to compose a playlist. The names of these songs are given in String[] songs, where each element is the name of a single song. Each name is unique and consists of at least three lowercase Latin letters.

A playlist is a sequence containing exactly K song names from songs. Each song from songs can be included zero, one or more times into a playlist. For each song in her playlist (except the last one), Elly wants the transition from this song to the next one in the playlist to be smooth. She calls transition from one song to another smooth if the substring formed by the first three letters of the name of the new song is the same as the substring formed by the last three letters from the name of the previous song. For example valid smooth transitions are from "xxxabc" to "abcyyy", from "entersandman" to "maneater", or from "heavensalie" to "liebe". On the other hand invalid transitions are from "yyydefg" to "defgyyy", from "toxicity" to "citylights", from "fadetoblack" to "breakingthehabit", from "hello" to "lol" or from "abbccddd" to "bcda".

Elly wonders how many different playlists with exactly K songs she can make from the given songs, while respecting her wish to have smooth transition between each two consecutive ones. Since this number can be quite large, return its remainder when divided by 1,000,000,007. Two playlists are considered different if there is an index i such that the i-th songs in these playlists are different.

Definition

Class:
EllysPlaylists
Method:
countPlaylists
Parameters:
String[], int
Returns:
int
Method signature:
int countPlaylists(String[] songs, int K)
(be sure your method is public)

Constraints

  • K will be between 1 and 1000, inclusive.
  • songs will contain between 1 and 50 elements, inclusive.
  • Each element of songs will contain between 3 and 20 characters, inclusive.
  • Each element of songs will consist of lowercase Latin letters ('a'-'z') only.
  • All elements of songs will be distinct.

Examples

  1. { "abcxxx", "xxxabc", "entersandman", "toxicity", "maneater", "heavensalie",
      "liebe", "citylights", "fadetoblack", "breakingthehabit", "yyydefg", "defgyyy" }
    2
    Returns: 5
    The possible pairs are {"abcxxx", "xxxabc"}, {"xxxabc", "abcxxx"}, {"entersandman", "maneater"}, {"heavensalie", "liebe"}, and {"defgyyy", "yyydefg"}. Note that the order of the songs is important.
  2. { "aaaaaa", "aaabcd", "bcdaaa" }
    4
    Returns: 13
    One song can be used more than once in a playlist.
  3. { "aaa", "aaaa", "aaaaa" }
    3
    Returns: 27
    Aaaaaaa...
  4. { "elly", "looks", "lovely" }
    1
    Returns: 3
    Each song is a valid playlist.
  5. { "life", "universe", "everything" }
    42
    Returns: 0
    No valid sequence of 42 songs exists.
  6. { "aaaaaaaaa", "aaabbbaaa", "aaacccaaa", "aaadddaaa", "aaaeeeaaa", "aaafffaaa" }
    12
    Returns: 176782322
    The answer can be quite large, so don't forget to return only its remainder.
Back to all problems