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

数据量不是很大 可以枚举

但是纯暴力的枚举可能会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;
}

最新文章

  1. javaScript系列:js中获取时间new Date()详细介绍
  2. Android利用Jsoup解析html 开发网站客户端小记。
  3. 【云计算】docker三剑客如何支持分布式部署?
  4. 【云计算】Cloudify-基于TOSCA规范的开源云应用编排系统
  5. 关于binary search的一点解惑
  6. boost静态链接的问题 -lgcc_s
  7. POJ 3164 Command Network 最小树形图
  8. uva 11489
  9. SqlDataReader类
  10. MFC 简单实现 DES 算法
  11. .NET MVC4 实训记录之四(Unit of work + Repository)
  12. mark_May
  13. 蓝桥网试题 java 算法训练 区间k大数查询
  14. Excel 转 vCard格式、CSV格式
  15. vue监听滚动事件-元素固定位置显示
  16. Cordova套网站
  17. Frosh Week HDU3743(逆序数)
  18. java-权限修饰符的区别
  19. java.util.Arrays$ArrayList addAll报错
  20. 【Java】 大话数据结构(16) 排序算法(3) (堆排序)

热门文章

  1. linux -- ubuntu桌面版安装xampp
  2. luasql在Fedora20下的安装与使用示例
  3. java jdk-awt.font在centos上中文乱码的问题, 安装中文字体
  4. mac 开发环境安装使用 记录
  5. 【java】 java 设计模式概述
  6. HTTP 错误 404.3 解决
  7. redisTools-IdGenerator
  8. Android Bigmap的操作(绘制,裁剪)
  9. quick-cocos2d-x游戏开发【10】——触摸捕获事件 cc.NODE_TOUCH_CAPTURE_EVENT
  10. ionic跳转(一)