链接:https://www.nowcoder.com/acm/contest/145/J
来源:牛客网
The first line of input contains two space-separated integers n, m (1 ≤ n, m ≤ 1000).
The next n lines contain m characters each, denoting the characters of the grid. Each character is an English letter (which can be either uppercase or lowercase).
Output a single integer, the number of sudoku-like subrectangles.
For simplicity, denote the j-th character on the i-th row as (i, j).1
For sample 1, there are 11 sudoku-like subrectangles. Denote a subrectangle
by (x
, y1
, x2
, y2
), where (x1
, y1
) and (x2
, y2
) are the upper-left and lower-right coordinates of the subrectangle.
The sudoku-like subrectangles are (1, 1, 1, 1), (1, 2, 1, 2), (1, 3, 1, 3), (2, 1, 2, 1), (2, 2, 2, 2), (2, 3, 2, 3), (1, 1, 1, 2), (1, 2, 1, 3), (2, 1, 2, 2), (1, 1, 2, 1), (1, 3, 2, 3).
For sample 2, the grid has 150 nonempty subrectangles, and all of them are sudoku-like.
题意 : 一个 n*m 的矩阵,求矩阵中子矩阵的个数,要求子矩阵中每行每列都没有相同的字母,求子矩阵的个数
思路分析 :
首先预处理两个东西, le[i][j] 表示 从点 (i, j) 向左最多可以延伸的单位 ,up[i][j] 表示从点 (i, j) 最多可以向上延伸的单位
len[i] 记录的是第 i 列的最大可以向上延伸的单位
代码示例 :
ll n, m; char s[1005][1005]; ll mp[1005][1005]; ll up[1005][1005], le[1005][1005]; void init() { for(ll j = 1; j <= m; j++){ // 列 ll num = 0; ll len = 0; for(ll i = 1; i <= n; i++){ // 行 if (!(num & ((1ll)<<mp[i][j]))) { num = num|((1ll)<<mp[i][j]); len++; } else { for(ll k = i-up[i-1][j]; k <= i-1; k++){ if (mp[k][j] == mp[i][j]) break; len--; ll x = (1ll)<<mp[k][j]; x = ~x; num &= x; } } up[i][j] = len; } } for(ll i = 1; i <= n; i++){ ll num = 0, len = 0; for(ll j = 1; j <= m; j++){ if (!(num & (1ll)<<mp[i][j])){ num = num|((1ll)<<mp[i][j]); len++; } else { for(ll k = j-le[i][j-1]; k <= j-1; k++){ if (mp[i][k] == mp[i][j]) break; len--; ll x = (1ll)<<mp[i][k]; x = ~x; num &= x; } } le[i][j] = len; } } } ll ans = 0; ll len[100]; void solve() { for(int j = 1; j <= m; j++){ memset(len, 0, sizeof(len)); for(int i = 1; i <= n; i++){ for(int k = 0; k < le[i][j]; k++){ len[k] = min(len[k]+1, up[i][j-k]); if (k) len[k] = min(len[k], len[k-1]); ans += len[k]; } for(int k = le[i][j]; k <= 55; k++) len[k] = 0; } } printf("%lld\n", ans); } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); cin >> n >> m; for(ll i = 1; i <= n; i++){ scanf("%s", s[i]+1); } for(ll i = 1; i <= n; i++){ for(ll j = 1; j <= m; j++){ if (s[i][j] >= ‘a‘ && s[i][j] <= ‘z‘) mp[i][j] = s[i][j]-‘a‘; else mp[i][j] = s[i][j]-‘A‘+26; } } init(); solve(); return 0; } /* 4 4 afcd bcda dddd abdc */
关于 le[i][j] 和 up[i][j] 两个数组,网上看到一个比较好的做法,很简便
for(int i = 1; i <= n; i++){ memset(pos, 0, sizeof(pos)); for(int j = 1; j <= m; j++){ L[i][j] = min(L[i][j-1] + 1, j - pos[s[i][j]]); pos[s[i][j]] = j; } } for(int j = 1; j <= m; j++){ memset(pos, 0, sizeof(pos)); for(int i = 1; i <= n; i++){ U[i][j] = min(U[i-1][j] + 1, i - pos[s[i][j]]); pos[s[i][j]] = i; } }
le[i][j] 的值来源于两种:一种是左边的值 le[i][j-1] + 1, 另一种是与其是同一个字母的时候两者间的距离,很简洁
原文:https://www.cnblogs.com/ccut-ry/p/9461711.html