牛客網多校賽第七場J--Sudoku Subrectangle

連接:https://www.nowcoder.com/acm/contest/145/J
來源:牛客網
 ios

時間限制: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?spa

 

輸入描述:

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.

示例1code

輸入

複製orm

2 3
AaA
caa

輸出

複製ci

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).

示例2get

輸入

複製input

4 5
abcde
fGhij
klmno
pqrst

輸出

複製string

150

說明

For sample 2, the grid has 150 nonempty subrectangles, and all of them are sudoku-like.

 

數據量不是很大 能夠枚舉it

可是純暴力的枚舉可能會T 由於是52*52*n*m

感受仍是挺考驗思惟的...反正我不是很會敲...看了題解以爲好巧妙

能夠優化到52*n*m

首先預處理出每一個位置距離上一個相同字母的距離    dp

而後枚舉每個點 每次往左走一格看看能多多少個長方形【與高度有關】,這個高度是不可能變大的

 

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;

int n, m;
const int maxn = 1005;
char grid[maxn][maxn];
int pos[maxn], L[maxn][maxn], U[maxn][maxn], len[maxn];
//pos 上一次該字母出現的位子 LU分別表示距離相同字母最近的距離

int main()
{
	while(scanf("%d%d", &n, &m) != EOF){
        for(int i = 1; i <= n; i++){
            scanf("%s", grid[i] + 1);
        }


        for(int i = 1; i <= n; i++){
            memset(pos, 0, sizeof(pos));
            for(int j = 1; j <= m; j++){
                L[i][j] = min(L[i][j - 1] + 1, j - pos[grid[i][j]]);
                pos[grid[i][j]] = j;
            }
        }

        for(int j = 1; j <= m; j++){
            memset(pos, 0, sizeof(pos));
            for(int i = 1; i <= n; i++){
                U[i][j] = min(U[i - 1][j] + 1, i - pos[grid[i][j]]);
                pos[grid[i][j]] = i;
            }
        }

        long long ans = 0;
        for(int j = 1; j <= m; j++){
            memset(len, 0, sizeof(len));
            for(int i = 1; i <= n; i++){
                for(int k = 0; k < L[i][j]; k++){
                    len[k] = min(len[k] + 1, U[i][j - k]);
                    if(k) len[k] = min(len[k], len[k - 1]);
                    ans += len[k];
                }
                for(int k = L[i][j]; k < 54; k++) len[k] = 0;
            }
        }
        printf("%lld\n", ans);
	}
	return 0;
}

        for(int j = 1; j <= m; j++){
            memset(pos, 0, sizeof(pos));
            for(int i = 1; i <= n; i++){
                U[i][j] = min(U[i - 1][j] + 1, i - pos[grid[i][j]]);
                pos[grid[i][j]] = i;
            }
        }

        long long ans = 0;
        for(int j = 1; j <= m; j++){
            memset(len, 0, sizeof(len));//len表示當前列能夠向上的長度
            for(int i = 1; i <= n; i++){
                for(int k = 0; k < L[i][j]; k++){
                    len[k] = min(len[k] + 1, U[i][j - k]);
                    if(k) len[k] = min(len[k], len[k - 1]);
                    ans += len[k];
                }
                for(int k = L[i][j]; k < 54; k++) len[k] = 0;
            }
        }
        printf("%lld\n", ans);
	}
	return 0;
}
相關文章
相關標籤/搜索