P5555 Order Spell.
Description
Modern magicians Xiao L and Xiao K are studying spells.
“Do you know how to use spells?”
“Of course. This is the most basic quality of a modern magician.”
“Then how much do you know about the history of spells?”
“I still remember some of what was taught in class. That was a very, very long time ago. Back then, no one in the world could use spells, and chaotic magic was the mainstream magic in the magic world. This was an evil technique: it needed no skill and no rules, and the darker one’s heart was, the stronger its power became. So the evil magicians killed each other, turning the world upside down, with blood flowing like rivers. Among them, a magician who called himself the ‘Chaos Demon’ led a group of magicians who used extremely dirty means to control almost the entire magic world, making it hard for those who longed for order and peace to survive. At that time, the savior of the savior of the world appeared. Two magicians named Laihemu La and Kenmaiduoka bravely stood up. With only the power of the two of them, they started a decisive battle with the Chaos Demon group, but in the end they were outnumbered and forced into a desperate situation. Just as the Chaos Demon’s final strike hit their bodies, Laihemu La and Kenmaiduoka used the huge magical power of that strike to elevate all their remaining magic and will into concepts, creating the Order Spell system, twisting the world’s theory so that chaotic magic was sealed forever. And the Chaos Demon was also reduced to ashes in this intense distortion. From then on, the magic world entered the Age of Order from the Age of Chaos. People followed the last wishes of these two saints, Laihemu La and Kenmaiduoka, and used spells under the Order Spell system, until now.”
“So that’s how it is. This is why we must follow a series of principles when using spells.”
“Yes. This is exactly the effort the two saints made to keep the world from falling back into the Age of Chaos. By the way, you only had your first magic class last week. Do you still remember the principles for using spells?”
“Let me think. First, it must appear in the order sequence. The system left by the two saints, after the unremitting efforts of later magicians, was translated into something called the order sequence. For the convenience of modern magicians, **the order sequence consists only of lowercase English letters**. Because the power of the system was too strong to be limited to only one sequence, **magicians transferred the wills of the two saints into two order sequences respectively**. A spell must be restricted by the order sequence. **Specifically, it must appear in the order sequence (it is a substring of the order sequence)**. Since the wills of the two saints are inseparable, **a spell must appear in both order sequences at the same time**. Second, in order to make the spell stable and accurate, the order system规定了魔咒的形态. **Specifically, the first character of the spell must be the same as the last character of the spell, the second character must be the same as the second-to-last character, and so on**. In this way, the spell becomes symmetric and orderly. As for more, let me see...”
“Stop looking, stop looking. The most important things are these. Also, you might not know: the longer the spell, the stronger its power.”
“Is that so? No wonder the spell the teacher demonstrated that day was so much more powerful than mine.”
“Yes, yes. Have you already noticed that there is an upper limit to the power of spells?”
“Ah, seems so. But the teacher said that the user of the strongest spell has not appeared yet?”
“Right. The user must have ability at the same level as the spell in order to use it smoothly. We beginners don’t know when we will reach that level...”
“Sigh... How about we count the length of the most powerful spell, and how many of them there are?”
“Sure. Anyway we have nothing to do, so let’s do something within our ability.”
So Xiao L and Xiao K started counting the length and number of the strongest spells. But before long, they could not hold on, because the order sequences were far, far too long.
Now, as a senior magician, you need to tell them this basic common knowledge. You already know the form of the two order sequences. Please help Xiao L and Xiao K compute the length and the number of the strongest spells.
Input Format
The first line contains two integers $n,m$, representing the lengths of the two order sequences.
Then the second and third lines contain two strings, representing the two order sequences, with lengths $n,m$ respectively.
Output Format
Output one line containing two integers separated by a single space, representing the length and the number of the strongest spells.
Explanation/Hint
**Sample Explanation**
Sample 1: The spells that satisfy the requirements are $a,b,aa,aaa,aba,bab$. Among them, the strongest are $aaa,aba,bab$, with length $3$, and there are $3$ of them.
Sample 2: The spells that satisfy the requirements are $a,b,aa,aaa,aaaa,bb,baab,aba,aabaa,c$. Among them, the strongest is $aabaa$, with length $5$, and there is $1$ of it.
**Constraints**
For some reason, this problem needs to use $Subtask$. To get the score of a $Subtask$, you need to pass all testdata points in this $Subtask$.
| | Score | Range of $n,m$ | Special Property |
| :----------: | :----------: | :----------: | :----------: |
| $Subtask1$ | $0$ | $1\le n,m\le260817$ | Samples |
| $Subtask2$ | $5$ | $1\le n,m\le260817$ | Both order sequences consist of the same single character |
| $Subtask3$ | $5$ | $1\le n,m\le10$ | None |
| $Subtask4$ | $10$ | $1\le n,m\le300$ | None |
| $Subtask5$ | $10$ | $1\le n,m\le2000$ | None |
| $Subtask6$ | $30$ | $1\le n,m\le260817$ | The two order sequences are identical |
| $Subtask7$ | $40$ | $1\le n,m\le260817$ | None |
Obviously, identical spells are counted only once. It is guaranteed that there exists at least one valid spell with length at least $1$.
Note that the time limit is $3s$.
Translated by ChatGPT 5