Codeforces Round 972 (Div. 2)
C
Lazy Narek
Codeforces
1800
原题
← B2D →
翻译中…

Narek is too lazy to create the third problem of this contest. His friend Artur suggests that he should use ChatGPT. ChatGPT creates $n$ problems, each consisting of $m$ letters, so Narek has $n$ strings. To make the problem harder, he combines the problems by selecting some of the $n$ strings possibly none and concatenating them without altering their order. His chance of solving the problem is defined as $score_n - score_c$, where $score_n$ is Narek's score and $score_c$ is ChatGPT's score.

翻译中…

Narek calculates $score_n$ by examining the selected string (he moves from left to right). He initially searches for the letter $\texttt{"n"}$, followed by $\texttt{"a"}$, $\texttt{"r"}$, $\texttt{"e"}$, and $\texttt{"k"}$. Upon finding all occurrences of these letters, he increments $score_n$ by $5$ and resumes searching for $\texttt{"n"}$ again (he doesn't go back, and he just continues from where he left off).

翻译中…

After Narek finishes, ChatGPT scans through the array and increments $score_c$ by $1$ for each letter $\texttt{"n"}$, $\texttt{"a"}$, $\texttt{"r"}$, $\texttt{"e"}$, or $\texttt{"k"}$ that Narek fails to utilize (note that if Narek fails to complete the last occurrence by finding all of the $5$ letters, then all of the letters he used are counted in ChatGPT's score $score_c$, and Narek doesn't get any points if he doesn't finish finding all the 5 letters).

翻译中…

Narek aims to maximize the value of $score_n - score_c$ by selecting the most optimal subset of the initial strings.

翻译中…

Input

翻译中…

In the first line of the input, you're given a single integer $t$ ($1 \le t \le 10^5$), the number of test cases. Then the description of each test case follows.

翻译中…

In the first line of each test case, you're given two integers $n, m$ ($1 \le n, m \le 10^3$), the number of strings and the length of each string.

翻译中…

In the next $n$ lines, you're given $n$ strings, each having a length of $m$. The strings only contain lowercase letters of the English alphabet.

翻译中…

The sum of values of $n \cdot m$ over all test cases does not exceed $10^6$.

翻译中…

Output

翻译中…

For each test case, output a single integer: the maximal possible value of $score_n - score_c$.

翻译中…

Note

翻译中…

In the first test case, one of the optimal answers is when Narek doesn't choose any of the strings, so the answer is $0$. He can alternatively choose all the strings. In this case, the full string becomes "nnaarreekk". Narek can choose the first appearances of all letters and add $5$ to the score. His opponent will add $1$ for all second appearances, which will be $5$ in total. So the answer will be $5 - 5 = 0$.

翻译中…

In the third test case, the only optimal answer is when Narek doesn't choose the string. Note that if he were to choose the string, he wouldn't be able to find the last letter "k", so his score would stay at $0$ instead of becoming $5$. Then ChatGPT would add $4$ for all of the $4$ letters, and the answer would become $0 - 4 = -4$.

翻译中…

In the last test case, Narek needs to choose the first and the last strings. After putting these two next to each other, he gets "${\color{red}{n}}rr{\color{red}{a}}{\color{red}{r}}{\color{red}{e}}{\color{red}{k}}{\color{red}{n}}k{\color{red}{a}}{\color{red}{r}}{\color{red}{e}}{\color{red}{k}}{\color{blue}{z}}$". Narek can choose the letters marked with red and add $10$ to his score. Since the black colored letters Narek left behind are eligible for the opponent to claim (they are used in the word "narek"), the opponent adds all other letters to the score and gets a score of $3$. Therefore, the answer is $10 - 3 = 7$.

翻译中…

样例

Input #1
4
5 2
nn
aa
rr
ee
kk
1 5
narek
1 4
nare
5 7
nrrarek
nrnekan
uuuuuuu
ppppppp
nkarekz
Output #1
0
5
0
7
InputCopy OutputCopy
题目信息
题号
2005C
时间限制
2 s
内存限制
256 MB
标签
动态规划
模拟
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。