NC24226. [USACO 2015 Ope B]Moocryption
描述
USOPEN OOMABO MOOMXO PQMROM
Being cows, their only word of interest is "MOO", which can appear in the word finder in many places, either horizontally, vertically, or diagonally. The example above contains 6 MOOs.
Farmer John is also a fan of word puzzles. Since the cows don't want him to solve their word finder before they have a chance to try it, they have encrypted its contents using a "substitution cipher" that replaces each letter of the alphabet with some different letter. For example, A might map to X, B might map to A, and so on. No letter maps to itself, and no two letters map to the same letter (since otherwise decryption would be ambiguous).
Unfortunately, the cows have lost track of the substitution cipher needed to decrypt their puzzle. Please help them determine the maximum possible number of MOOs that could exist in the puzzle for an appropriate choice of substitution cipher.
输入描述
The first line of input contains N and M, describing the number of rows and columns of the puzzle (both are at most 50). The next NN lines each contain M characters, describing one row of the encrypted puzzle. Each character is an uppercase letter in the range A..Z.
输出描述
Please output the maximum possible number of MOOs contained in the puzzle if decrypted with an appropriate substitution cipher.
示例1
输入:
4 6 TAMHGI MMQVWM QMMQSM HBQUMQ
输出:
6
说明:
This is the same puzzle at the beginning of the problem statement after a cipher has been applied. Here "M" and "O" have been replaced with "Q" and "M" respectively.C++14(g++5.4) 解法, 执行用时: 2ms, 内存消耗: 380K, 提交时间: 2020-08-23 20:14:39
#include <bits/stdc++.h> #define ll long long const int maxn=1e5+10; using namespace std; int n,m,k,t,ma,p[300][300]; char a[51][51]; int dis[][2]={0,1,0,-1,1,0,-1,0,1,-1,1,1,-1,1,-1,-1}; void check(int x,int y) { for(int i=0;i<8;i++) { int s[2],t[2]; s[0]=x+dis[i][0]; s[1]=x+dis[i][0]*2; t[0]=y+dis[i][1]; t[1]=y+dis[i][1]*2; if(s[1]>=0&&s[1]<n&&t[1]>=0&&t[1]<m&&a[x][y]!=a[s[0]][t[0]]&&a[s[0]][t[0]]==a[s[1]][t[1]]&&a[x][y]!='M'&&a[s[0]][t[0]]!='O') ma=max(ma,++p[a[x][y]][a[s[0]][t[0]]] ); } } int main() { int i,j; scanf("%d%d",&n,&m); for(i=0;i<n;i++)scanf("%s",a[i]); for(i=0;i<n;i++) for(j=0;j<m;j++) { check(i,j); } printf("%d\n",ma); //system("pause"); return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 492K, 提交时间: 2020-03-14 22:13:12
#include<bits/stdc++.h> #define ll long long const int maxn=1e5+10; using namespace std; int n,m,k,t,ma,p[300][300]; char a[51][51]; int dis[][2]={0,1,0,-1,1,0,-1,0,1,-1,1,1,-1,1,-1,-1}; void check(int x,int y) { for(int i=0;i<8;i++) { int s[2],t[2]; s[0]=x+dis[i][0]; s[1]=x+dis[i][0]*2; t[0]=y+dis[i][1]; t[1]=y+dis[i][1]*2; if(s[1]>=0&&s[1]<n&&t[1]>=0&&t[1]<m&&a[x][y]!=a[s[0]][t[0]]&&a[s[0]][t[0]]==a[s[1]][t[1]]&&a[x][y]!='M'&&a[s[0]][t[0]]!='O') ma=max(ma,++p[a[x][y]][a[s[0]][t[0]]]); } } int main() { int i,j; scanf("%d%d",&n,&m); for(i=0;i<n;i++) scanf("%s",a[i]); for(i=0;i<n;i++) for(j=0;j<m;j++) { check(i,j); } printf("%d\n",ma); return 0; }
pypy3(pypy3.6.1) 解法, 执行用时: 86ms, 内存消耗: 25784K, 提交时间: 2020-08-23 23:41:22
#!/usr/bin/env python3 # # Moocryption # import sys, os from collections import defaultdict def read_ints(): return list(map(int, input().split())) n, m = read_ints() a = [input().strip() for _ in range(n)] c = defaultdict(int) for i in range(n): for j in range(m): for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]: if 0 <= (i + dx + dx) < n and 0 <= (j + dy + dy) < m and \ a[i][j] != a[i + dx][j + dy] and a[i + dx][j + dy] == a[i + dx + dx][j + dy + dy]: c[(a[i][j], a[i + dx][j + dy], a[i + dx + dx][j + dy + dy])] += 1 ans = 0 for (x, y, _), v in c.items(): if x != 'M' and y != 'O': ans = max(ans, v) print(ans)