Sunteți pe pagina 1din 2

Sectumsempra

The frequency of Hermiones visits to the library has considerably increased. This is not surprising because the Ordinary Wizarding Levels (OWLs) are a few months away. And as usual, Hermione has been preparing for her Charms exam way ahead of schedule. Curiously one evening, she chanced upon this mysterious book in the library on the theory of Spells. What was of particular interest to the studious girl was the description of the effect of a spell i seconds after it had been cast. A spell is an arbitrary sequence of magical symbols (represented as lower case letters [a-z] for convenience). The effect of a spell after i seconds is the length x of the longest sequence of symbols from the ith position onward that matches with the symbols at the start of the spell. As she sat chewing her pencil, she was struck by an idea for the invention of her very own hybrid spell. She decided to first choose some standard spells of equal length as the base spells. These base spells would then be used to devise her new spell one symbol at a time. Specifically, the ith symbol of the new spell would be the ith symbol of some base spell. Encouraged, Hermiones mind is racing. She wonders what would happen if she choose the ith character from that spell which has maximum effect after i seconds. She knows enough about magic to understand that these new spells would be Spells of Power, but is worried they might turn out to be evil in nature. To ease her mind, she asks Harry and Ron to investigate the Spells of Power that can be devised using her technique. But they need two things to start off - the lexicographically smallest such Spell of Power, and the total number of distinct Spells of Power that can be devised by her method. Help them!
Input Format

The first line of the input contains 2 integers N and M - the length of each spell and the number of spells respectively. M lines follow such that each consists of a spell of exactly N symbols (lowercase characters) Output Format The output should contain an integer equal to the number of possible Spells of Power followed by the lexicographically smallest spell possible (separated by a single space). As the number of spells may be too large the output should be given modulo 10000007. Constraints N<=10000 M<=200 Sample Input 53 asdad ppapp badbb

Sample Output 12 apapb Explanation The effect sequence of each spell is: asdad - a-5 s-0 d-0 a-1 d-0 ppapp - p-5 p-1 a-0 p-2 p-1 badbb - b-5 a-0 d-0 b-1 b-1 Hybrid: a/p/bp Choices: 3 1 a/d 2 p 1 p/b 2

Number of spells of power = 3*1*2*1*2 = 12 Thus, the strongest spells that can be generated are apdpp, apdpb, apapp, apapb, ppdpp, ppdpb, ppapp, ppapb, bpdpp, bpdpb, bpapp, bpapb among which apapb is lexicographically the smallest.

S-ar putea să vă placă și