牛客暑期多校 - 寻找子矩阵的个数

链接:https://www.nowcoder.com/acm/contest/145/J
来源:牛客网

题目描述

You have a n * m grid of characters, where each character is an English letter (lowercase or uppercase, which means there are a total of 52 different possible letters).

A nonempty subrectangle of the grid is called sudoku-like if for any row or column in the subrectangle, all the cells in it have distinct characters.

How many sudoku-like subrectangles of the grid are there?

输入描述:

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.
示例1

输入

复制
2 3
AaA
caa

输出

复制
11

说明

For simplicity, denote the j-th character on the i-th row as (i, j).

For sample 1, there are 11 sudoku-like subrectangles. Denote a subrectangle
by (x
1
, y
1
, x
2
, y
2
), where (x
1
, y
1
) and (x
2
, y
2
) 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).
示例2

输入

复制
4 5
abcde
fGhij
klmno
pqrst

输出

复制
150

说明

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, 另一种是与其是同一个字母的时候两者间的距离,很简洁

猜你喜欢

转载自www.cnblogs.com/ccut-ry/p/9461711.html