This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
const int N = 1005;
string s[N], a[2];
int f(int i, int j){
if (i < 0 || i >= n - 1) return 0;
if (j < 0 || j >= m - 1) return 0;
return s[i][j] == a[0][0] && s[i + 1][j] == a[1][0] && s[i][j + 1] == a[0][1] && s[i + 1][j + 1] == a[1][1];
}
int add(int i, int j, int x){
int ans = 0;
ans += f(i, j);
ans += f(i - 1, j);
ans += f(i, j - 1);
ans += f(i - 1, j - 1);
return ans * x;
}
void Solve()
{
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
cin >> a[0] >> a[1];
int ans = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
ans += f(i, j);
}
}
int og = ans;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
char temp = s[i][j];
int curr = og;
curr += add(i, j, -1);
s[i][j] = 'J';
ans = max(ans, curr + add(i, j, 1));
s[i][j] = 'O';
ans = max(ans, curr + add(i, j, 1));
s[i][j] = 'I';
ans = max(ans, curr + add(i, j, 1));
s[i][j] = temp;
}
}
cout << ans << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |