#include <bits/stdc++.h>
using namespace std;
const int N = 1 * 1010;
const int MN = - N * N - 10;
const char cc[6] = {'n','a','r','e','k'};
string s[N];
int dp[6][2];
int T,n,m;
int pre(int x)
{
return (x + 1) % 2;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
cin >> s[i];
int now = 0;
for(int i = 0;i < 5;i++)
dp[i][now] = MN;
dp[4][now] = 0;
for(int k = 1;k <= n;k++)
{
int sum = 0;
for(int i = 0;i < m;i++)
{
int flag = 0;
for(int j = 0;j < 5;j++)
if(s[k][i] == cc[j])
flag = 1;
sum += flag;
}
now = (now + 1) % 2; //
for(int i = 0;i < 5;i++)
dp[i][now] = dp[i][pre(now)];
int nxt;
for(int bg = 0;bg < 5;bg++)
{
if(dp[bg][pre(now)] == MN)continue;
nxt = (bg + 1) % 5;
int count = 0;
for(int j = 0;j < m;j++)
{
if(s[k][j] == cc[nxt])
{
count ++;
dp[nxt][now] = max(dp[nxt][now] ,dp[bg][pre(now)] + count - (sum - count));
nxt = (nxt + 1) % 5;
}
}
}
}
int ans = -1;
for(int i = 0;i < 5;i++)
ans = max(ans,dp[i][now] - ((i + 1) % 5) * 2);
printf("%d\n",ans);
}
return 0;
}
Codeforces Round 972 (Div. 2) C
相關文章
- Codeforces Round 972 (Div. 2)
- Codeforces Round 972 (Div. 2) 補題記錄(A~C,E1)
- Codeforces Round #453 (Div. 2) C
- Codeforces Round #452 (Div. 2) C
- Codeforces Round 973 (Div. 2) C
- Codeforces Round 982 (Div. 2)(A~C)
- Codeforces Round #321 (Div. 2) C DFS
- Educational Codeforces Round 33 (Rated for Div. 2) C
- Codeforces Round #323 (Div. 2) C gcdGC
- Codeforces Round #325 (Div. 2) C 模擬
- Codeforces Round #359 (Div. 2) C DFS
- Codeforces Round #290 (Div. 2) A,B,C,D
- codeforces Round #252 (Div. 2) C - Valera and Tubes
- Codeforces Round #250 (Div. 2) C、The Child and Toy
- Codeforces Round 977 (Div. 2)(B-C2)
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #448 (Div. 2) A
- Codeforces Round #217 (Div. 2)
- Codeforces Round #256 (Div. 2)
- Codeforces Round #259 (Div. 2)
- Codeforces Round #257 (Div. 2)
- Codeforces Round #258 (Div. 2)
- Codeforces Round #171 (Div. 2)
- Codeforces Round #173 (Div. 2)
- Codeforces Round 951 (Div. 2)
- Codeforces Round 955 (Div. 2)
- Codeforces Round 953 (Div. 2)
- Codeforces Round 975 (Div. 2)
- Codeforces Round 976 (Div. 2)
- Codeforces Round 979 (Div. 2)
- Codeforces Round 982 (Div. 2)
- Codeforces Round 932 (Div. 2)