Cocktail Party
Time limit1sMemory limit1024 MB
For each length r from 0 to n-1, count the pairs of cups whose substrings of length r match, and find the largest product of their deliciousness values.
- Level
Medium7 of 10
- Topics
- String, String matching, Union-find, Sorting
- Solved
- No attempts yet
Problem
You are given a string of length with the labels of cups of cocktail. The -th cup has label , and every label is one of the 26 lowercase English letters. Let be the string formed by the labels from cup to cup . If where , , , and , then cup and cup are called -similar. If two cups are -similar for some , they are also -similar, -similar, ..., and -similar. For any with , cups and are -similar.
Freda assigns a deliciousness value to each cup. Mixing cup and cup produces a cocktail with deliciousness . For each , the problem asks how many pairs of cups are -similar, and the maximum deliciousness obtainable by mixing two -similar cups.
Input
The first line contains an integer , the number of cups. The second line contains a string of length , where the -th character is the label of cup . The third line contains integers separated by single spaces, where the -th integer is .
Output
Print lines. The -th line contains two integers: the number of pairs of -similar cups, and the maximum deliciousness from mixing two -similar cups. If no two cups are -similar, print 0 0 on that line.
Constraints