GRE - GRE Words
Recently George is preparing for the Graduate Record Examinations (GRE for short). Obviously the most important thing is reciting the words.
Now George is working on a word list containing N words. He has so poor a memory that it is too hard for him to remember all of the words on the list. But he does find a way to help him to remember. He finds that if a sequence of words has a property that for all pairs of neighboring words, the previous one is a substring of the next one, then the sequence of words is easy to remember.
So he decides to eliminate some words from the word list first to make the list easier for him. Meantime, he doesn't want to miss the important words. He gives each word an importance, which is represented by an integer ranging from -1000 to 1000, then he wants to know which words to eliminate to maximize the sum of the importance of remaining words. Negative importance just means that George thought it useless and is a waste of time to recite the word.
Note that although he can eliminate any number of words from the word list, he can never change the order between words. In another word, the order of words appeared on the word list is consistent with the order in the input. In addition, a word may have different meanings, so it can appear on the list more than once, and it may have different importance in each occurrence.
Input
The first line contains an integer T (1 ≤ T ≤ 50), indicating the number of test cases.
Each test case contains several lines. The first line contains an integer N (1 ≤ N ≤ 2×104), indicating the number of words.
Then N lines follows, each contains a string Si and an integer Wi, representing the word and its importance. Si contains only lowercase letters.
You can assume that the total length of all words will not exceeded 3×105.
Output
For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is the largest importance of the remaining sequence of words.
Example
Input: 1 5 a 1 ab 2 abb 3 baba 5 abbab 8 Output: Case #1: 14There was a serious and subtle problem regarding the test cases. It has been fixed and all submissions were rejudged now.
hide comments
cyl001:
2023-09-30 02:40:13
My code is AC here but REin UVA |
|
jjsnam:
2022-12-17 09:24:25
毒瘤题。 |
Added by: | Bin Jin |
Date: | 2011-11-08 |
Time limit: | 2.373s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
Resource: | ACM/ICPC Regional Contest, Chengdu 2011 |