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 (x1, 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).

示例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个所以在寻找时行列长度都不会超过52,很简单的一个52*52*nm;

           为了避免超时,题目需要预处理,使其变成52*nm;

           以左上角点为起点,寻找每一行不重复的长度,记录,再用另一个数组去记录从该点开始最长的行数;

           因为是从每个左上角点记录的,所以首先加上这一行无重复的长度;

           在从这个点开始,按列遍历,记录每一行的高度(选择最小的记录);

           然后从当前点开始,遍历每一行,如果这一行的最远的那个点处的高度符合要求(即大于等于行的长度),就把长度加进去,否则点往左挪一位,如果点向左越过了开始点的界限,则退出;

           

#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
char str[maxn][maxn];
int lenth[maxn][maxn],wide[maxn][maxn],flag[maxn],differ[maxn][maxn];
int main()
{
    int n,m,cnt=0,k;
    long long ans=0;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",str[i]+1);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cnt++;
            for(k=j;k<=min(j+52-1,m);k++)
            {
                if(flag[str[i][k]]==cnt)
                {
                    break;
                }

                    flag[str[i][k]]=cnt;//记录列的长度

            }
            wide[i][j]=k-j;
            cnt++;//因为cnt值在变化,所以不同的元素可以借助这个判断,所以不用每次把数组清空
            for(k=i;k<=min(i+52-1,n);k++)
            {
                if(flag[str[k][j]]==cnt)
                {
                    break;
                }

                    flag[str[k][j]]=cnt;
            }
            lenth[i][j]=k-i;//记录行的长度
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            ans+=wide[i][j];
            differ[i][j]=lenth[i][j];
            for(k=j+1;k<=(wide[i][j]+j-1);k++)//从该行起,每一列最短的行数记录
            {
                differ[i][k]=min(lenth[i][k],differ[i][k-1]);
            }
            int now=j+wide[i][j]-1;
            for(k=i+1;k<=(lenth[i][j]+i-1);k++)
            {
                now=min(now,j+wide[k][j]-1);//now代表了点的位置
                while(differ[i][now]<(k-i+1)&&now>=j)
                {
                    now--;
                }
                if(now<j)
                {
                    break;
                }
                ans+=now-j+1;
            }
        }
    }
    cout<<ans<<endl;
}

           

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值