member srm 471 div2 500 (dp)


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 vector <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:
vector <string>, int
Returns:
int
Method signature:
int countPlaylists(vector <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
0)

   
{ "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.
1)

   
{ "aaaaaa", "aaabcd", "bcdaaa" }
4
Returns: 13
One song can be used more than once in a playlist.
2)

   
{ "aaa", "aaaa", "aaaaa" }
3
Returns: 27
Aaaaaaa...
3)

   
{ "elly", "looks", "lovely" }
1
Returns: 3
Each song is a valid playlist.
4)

   
{ "life", "universe", "everything" }
42
Returns: 0
No valid sequence of 42 songs exists.
5)

   
{ "aaaaaaaaa", "aaabbbaaa", "aaacccaaa", "aaadddaaa", "aaaeeeaaa", "aaafffaaa" }
12
Returns: 176782322
The answer can be quite large, so don't forget to return only its remainder.
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.

 

一个不难的dp,当时根本没往dp上想……

记录以每一个字符串结束的次数。

morefreeze大牛代码精练,佩服……

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值