牛客网暑期ACM多校训练营(第七场) J Sudoku Subrectangles(模拟)

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

Sudoku Subrectangles

时间限制: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.

题意:给你一个n*m的字符串矩阵(只包含大小写字母),若其中的某个子矩形每一行、每一列字母都不相同,则称这个矩形

sudoku-like”。求有多少个这样的子矩形。

思路:比赛的时候读错题了,以为子矩形所有元素都不相同。。。实际上只需要每行每列都没有相同的字符即可。。。那这就是道暴力模拟了啊。。。时间复杂度n*m*52。。。常数写大一点也没关系。。数据比较弱。。。我说怎么过得人数那么多。。。暴力求每个点能到达的最远的右面和下面的位置,然后枚举每个点为矩形左上角顶点,再依次枚举矩形上下两条边的长度然后求剩余两条边的长度,累加答案即可。

代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1010;
int m,n,k;
char s[maxn][maxn];
int id,r[maxn][maxn],d[maxn][maxn];
int b[maxn];
int stk[maxn];
ll ans;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(b,0,sizeof(b));
         memset(d,0,sizeof(d));
          memset(r,0,sizeof(r));
        id=0;ans=0;
        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++)
        {
            id++;
            for(int k=j;k<=m;k++)
            {
                if(b[s[i][k]]==id)
                {
                    r[i][j]=k-1;
                    break;
                }
                b[s[i][k]]=id;
            }
            if(!r[i][j]) r[i][j]=m;

            id++;
            for(int k=i;k<=n;k++)
            {
                if(b[s[k][j]]==id)
                {
                    d[i][j]=k-1;
                    break;
                }
                b[s[k][j]]=id;
            }
            if(!d[i][j]) d[i][j]=n;
        }
        memset(stk,0,sizeof(stk));
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            for(int k=i;k<=d[i][j];k++)
            {
                stk[k]=r[k][j];
                if(k>i&&stk[k]>stk[k-1])
                stk[k]=stk[k-1];
            }
            int top=d[i][j];//???
            for(int k=j;k<=r[i][j];k++)
            {
                if(top>d[i][k]) top=d[i][k];
                while(top>=i&&stk[top]<k)top--;
                ans+=(ll)(top-i+1);
                //cout<<top<<" "<<i<<endl;
            }
        }
        printf("%lld\n",ans);
    }
   return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值