Advertisement
Guest User

Autocomplete Strikes Back

a guest
Jan 25th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. This morning you woke up with an uncontrollable urge to send a text message made up of K distinct words. But, can you believe it? Your new phone just crashed and all of the words are missing from its dictionary! You used to have N words in there, and you certainly don't have time to add all of them back right now.
  2.  
  3. Your plan is to just choose K of the N possible words, add them to your phone's dictionary, and then text each of them. To text a certain word, you must either type the word itself, or any nonempty prefix of it which is not a prefix of any other word in the dictionary.
  4.  
  5. What's the minimum number of letters you must type to send your message of K words?
  6.  
  7. Input
  8. Input begins with an integer T, the number of test cases. For each test case, there is first a line containing the space-separated integers N and K. Then, N lines follow, each containing a word that used to be in your phone's dictionary.
  9.  
  10. Output
  11. For the ith test case, print a line containing "Case #i: " followed by the minimum number of characters you need to type to send your text message.
  12.  
  13. Constraints
  14. 1 ≤ T ≤ 20
  15. 2 ≤ N ≤ 4,000
  16. 1 ≤ K ≤ min(N-1, 100)
  17. The N words will have a total length of no more than 20,000 characters.
  18. The words are made up of only lower-case alphabetic characters.
  19. The words are pairwise distinct.
  20. Explanation of Sample
  21. In the first case, one option is to choose the words "tin", "tinny", "gigantic", and "tilts". You can then text these words by typing "tin", "tinn", "g", and "til", respectively, for a total of 3+4+1+3=11 letters.
  22.  
  23.  
  24. INPUT
  25.  
  26. 5
  27. 6 4
  28. tin
  29. tiny
  30. tinny
  31. gigantic
  32. tilt
  33. tilts
  34. 3 2
  35. apple
  36. apricot
  37. cherry
  38. 5 3
  39. a
  40. aa
  41. aaa
  42. aaaa
  43. aaaaa
  44. 5 3
  45. the
  46. quick
  47. brown
  48. fox
  49. jumped
  50. 8 7
  51. cork
  52. work
  53. card
  54. ward
  55. font
  56. front
  57. word
  58. sword
  59.  
  60.  
  61. OUTPUT
  62.  
  63. Case #1: 11
  64. Case #2: 2
  65. Case #3: 6
  66. Case #4: 3
  67. Case #5: 13
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement