牛客网暑期ACM多校训练营(第七场)J:Sudoku Subrectangles(思维)

链接:https://www.nowcoder.com/acm/contest/145/J

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
Special Judge, 64bit IO Format: %lld
题目描述
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.

思路:因为只有52个不同的字母,所以对于每一个格子 ( i , j ) ,可以求出以 ( i , j ) 为子矩形的一个角的满足条件的矩形个数。

先预处理 L [ i ] [ j ] 表示从 ( i , j ) 开始向左能延伸的最长长度。(即没有出现重复字母)
U [ i ] [ j ] 表示从 ( i , j ) 开始向上能延伸的最长长度。

然后枚举每个 ( i , j ) ,枚举矩形。

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e3+10;
const int MOD=1e9+7;
typedef long long ll;
char s[MAX][MAX];
int a[MAX][MAX];
int L[MAX][MAX];
int U[MAX][MAX];
int len[MAX];
int n,m;
void init()//预处理L[i][j] U[i][j]
{
    for(int i=1;i<=n;i++)
    {
        int l=1,r=1;
        ll sum=0;
        while(r<=m)
        {
            while(l<r&&(sum&(1ll<<a[i][r])))sum^=1ll<<a[i][l],l++;
            L[i][r]=r-l+1;
            sum^=1ll<<a[i][r];
            r++;
        }
    }
    for(int j=1;j<=m;j++)
    {
        int l=1,r=1;
        ll sum=0;
        while(r<=n)
        {
            while(l<r&&(sum&(1ll<<a[r][j])))sum^=1ll<<a[l][j],l++;
            U[r][j]=r-l+1;
            sum^=1ll<<a[r][j];
            r++;
        }
    }
}
ll cal(int x,int y)
{
    for(int i=y;i>=y-L[x][y]+1;i--)len[i]=(i==y?U[x][i]:min(len[i+1],U[x][i]));//len[i]表示第i列向上延伸的最长长度
    ll tot=0;
    int LL=y-L[x][y]+1;  //表示当前行向左延伸的最长长度
    for(int i=x;i>=1;i--)
    {
        LL=max(LL,y-L[i][y]+1);//更新向左延伸的最长长度
        while(LL<=y&&len[LL]<x-i+1)LL++;//必须满足len[LL]>=x-i+1 即向上延伸的长度覆盖了当前行才能形成矩形
        tot+=y-LL+1;
        if(LL>y)break;
    }
    return tot;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    {
        if(s[i][j]>='a'&&s[i][j]<='z')a[i][j]=s[i][j]-'a'+0;
        else a[i][j]=s[i][j]-'A'+26;
    }
    init();
    ll ans=0;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)ans+=cal(i,j);
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mitsuha_/article/details/81543681