#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 973 (Div. 2) C
- Codeforces Round 982 (Div. 2)(A~C)
- Codeforces Round 986 (Div. 2) A-C
- Codeforces Round 977 (Div. 2)(B-C2)
- 【Codeforces Round #437 (Div. 2) C】 Ordering Pizza
- Codeforces Round #541 (Div. 2)
- Codeforces Round 940 (Div. 2)
- Codeforces Round 934 (Div. 2)
- Codeforces Round 932 (Div. 2)
- Codeforces Round 948 (Div. 2)
- Codeforces Round #639 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round 987 (Div. 2)
- Codeforces Round 976 (Div. 2)
- Codeforces Round 975 (Div. 2)
- Codeforces Round 986 (Div. 2)
- Codeforces Round 973 (Div. 2)
- Codeforces Round 969 (Div. 2)
- Codeforces Round 873 (Div. 2)
- Codeforces Round 967 (Div. 2)
- Codeforces Round 965 (Div. 2)
- Codeforces Round 963 (Div. 2)
- Codeforces Round 979 (Div. 2)
- Codeforces Round 949 (Div. 2)
- Codeforces Round 955 (Div. 2)
- Codeforces Round 951 (Div. 2)
- Codeforces Round 961 (Div. 2)
- Codeforces Round 958 (Div. 2)
- Codeforces Round 960 (Div. 2)
- Codeforces Round 953 (Div. 2)
- Codeforces Round 982 (Div. 2)
- Codeforces Round 945 (Div. 2)
- Codeforces Round #747 (Div. 2)