牛客多校第七场 Sudoku Subrectangles (dp+遍历+预处理)

 

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

题意 找出满足每一行每一列都没有重复的子矩阵个数

题解 遍历右下端点求出最上能到达距离 和最左能到达距离 因为每行每列都不该有重复数字,每个点的最远距离应该是同行或同列前一个点的min值 简单dp吧 然后遍历每个点以这个点为右下端点根据最上和最左到达距离 行列交点可能重复,可以求出52*52*n*m会超时,到时讲道理一般情况下是不可能的 除非是每行每列都是52个52个循环才可能超时,但就是这样卡了,后来发现如果根据向上能到达距离,发现每个点的向左能到达距离是单调的, 所以先向左遍历,找到每个点上方能到达的最远距离,和前一个比取小的求出最右到达距离,然后向上遍历加起来就是答案。

#include<bits/stdc++.h>
using namespace std;
const double PI=acos(-1.0);
typedef long long ll;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define clr(x) memset(x,0,sizeof(x))
const int INF=0x3f3f3f3f;
char a[1005][1005];
struct pos{
    int x,y;
}b[1005][1005];
int main(){
    //本地测试
    #ifdef ONLINE_JUDGE
    #else
    freopen("C://Users//yan//Desktop//in.txt","r",stdin);
    #endif
    ios::sync_with_stdio(false);//取消同步
    std::cin.tie(0);//解除cin与cout的绑定,进一步加快执行效率。
    int n,m;
    cin>>n>>m;
    rep(i,1,n+1){
        rep(j,1,m+1){
        	cin>>a[i][j];
		}
    }
    rep(i,1,n+1){
        rep(j,1,m+1){
        	b[i][j].y=0;
            for(int k=1;k<52&&k<i;k++){
                if(a[i][j]!=a[i-k][j]){
                    b[i][j].y=k;
                }
                else{
                    break;
                }
            }
            for(int k=1;k<52&&k<j;k++){
                if(a[i][j]!=a[i][j-k]){
                    b[i][j].x=k;
                }
                else{
                    break;
                }
            }
            b[i][j].x=min(b[i][j].x,b[i][j-1].x+1);
            b[i][j].y=min(b[i][j].y,b[i-1][j].y+1);
        }
    }
//  rep(i,1,n+1){
//      rep(j,1,m+1){
//          cout<<b[i][j].x<<","<<b[i][j].y<<"   ";
//      }
//      cout<<endl;
//  }
    ll sum=0;
    int l[52];
    rep(i,1,n+1){
        rep(j,1,m+1){
            sum+=b[i][j].x+b[i][j].y;
            int temp=b[i][j].x;
            clr(l);
            l[1]=b[i][j-1].y;
            rep(k,2,b[i][j].x+1)
            	l[k]=min(l[k-1],b[i][j-k].y);
            int now=b[i][j].x;
            rep(k,1,b[i][j].y+1){
            	while(l[now]<k&&now>0)
            		now--;
            	temp=min(temp,min(b[i-k][j].x,now));
                sum+=temp;
            }
        }
    }
    cout<<sum+n*m<<endl;
}

猜你喜欢

转载自blog.csdn.net/remarkableyan/article/details/81561811