牛客第七场 Sudoku Subrectangles

链接: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.


预处理每一个点向下想右最大的延伸长度,然后再遍历一遍所有点,枚举所有点是否成立的情况
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e3+10;
const ll mod = 1e9+7;
const double pi = acos(-1.0);
const double eps = 1e-8;
ll n, m, lmax[maxn][maxn], pos[maxn];
ll umax[maxn][maxn], len[maxn];
char s[maxn][maxn];
int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    scanf("%lld%lld",&n,&m);
    for( ll i = 1; i <= n; i ++ ) {
        scanf("%s",s[i]+1);
    }
    for( ll i = 1; i <= n; i ++ ) {
        memset(pos,0,sizeof(pos));
        for( ll j = 1; j <= m; j ++ ) {
            lmax[i][j] = min(lmax[i][j-1]+1,j-pos[s[i][j]]);
            pos[s[i][j]] = j;
        }
    }
    for( ll j = 1; j <= m; j ++ ) {
        memset(pos,0,sizeof(pos));
        for( ll i = 1; i <= n; i ++ ) {
            umax[i][j] = min(umax[i-1][j]+1,i-pos[s[i][j]]);
            pos[s[i][j]] = i;
        }
    }
    ll ans = 0;
    for( ll r = 1; r <= m; r ++ ) {
        memset(len,0,sizeof(len));
        for( ll d = 1; d <= n; d ++ ) {
            for( ll i = 0; i < lmax[d][r]; i ++ ) {
                len[i] = min(umax[d][r-i],len[i]+1);
                if(i) {
                    len[i] = min(len[i],len[i-1]);
                }
                ans += len[i];
            }
            for( ll i = lmax[d][r]; i <= 54; i ++ ) {
                len[i] = 0;
            }
        }
    }
    printf("%lld
",ans);
    return 0;
}

  

 
彼时当年少,莫负好时光。
原文地址:https://www.cnblogs.com/l609929321/p/9454143.html