链接: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;
}

牛客网多校赛第七场J--Sudoku Subrectangle的更多相关文章

  1. 牛客网多校赛第七场--C Bit Compression【位运算】【暴力】

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言524 ...

  2. 牛客网多校赛第七场A--Minimum Cost Perfect Matching【位运算】【规律】

    链接:https://www.nowcoder.com/acm/contest/145/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  3. 牛客网多校赛第9场 E-Music Game【概率期望】【逆元】

    链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  4. 牛客网-湘潭大学校赛重现H题 (线段树 染色问题)

    链接:https://www.nowcoder.com/acm/contest/105/H来源:牛客网 n个桶按顺序排列,我们用1~n给桶标号.有两种操作: 1 l r c 区间[l,r]中的每个桶中 ...

  5. 牛客网多校赛第九场A-circulant matrix【数论】

    链接:https://www.nowcoder.com/acm/contest/147/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  6. 牛客网多校训练第四场C sequence

    (牛客场场有笛卡尔树,场场都不会用笛卡尔树...自闭,补题心得) 题目链接:https://ac.nowcoder.com/acm/contest/884/C 题意:给出两个序列a,b,求max{mi ...

  7. 牛客网多校训练第三场 C - Shuffle Cards(Splay / rope)

    链接: https://www.nowcoder.com/acm/contest/141/C 题意: 给出一个n个元素的序列(1,2,...,n)和m个操作(1≤n,m≤1e5),每个操作给出两个数p ...

  8. 牛客网多校训练第三场 A - PACM Team(01背包变形 + 记录方案)

    链接: https://www.nowcoder.com/acm/contest/141/A 题意: 有n(1≤n≤36)个物品,每个物品有四种代价pi,ai,ci,mi,价值为gi(0≤pi,ai, ...

  9. 牛客网多校训练第八场A All one Matrix

    题目链接:https://ac.nowcoder.com/acm/contest/888/A 题意:求出有多少个不被包含的全1子矩阵 解题思路:首先对列做处理,维护每个位置向上1的个数,然后我们从最后 ...

随机推荐

  1. 【转】DirectorySearcher.Filter属性说明

    DirectorySearcher.Filter属性扩充申明 DirectorySearcher mySearcher = new DirectorySearcher(entryOU, "( ...

  2. PHP 正则表达式 及常用汇总

    Δ  定界符 Δ  字符域 Δ  修饰符 Δ  限定符 Δ  脱字符 Δ  通配符(正向预查,反向预查) Δ  反向引用 Δ  惰性匹配 Δ  注释 Δ  零字符宽     1.    平时做网站经常 ...

  3. C++ 类 & 对象

    C++ 类 & 对象C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程序设计.类是 C++ 的核心特性,通常被称为用户定义的类型. 类用于指定对象的形式,它包含了数据表示法 ...

  4. nodejs基础 -- 回调函数

    Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了. 回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,Node 所有 API 都 ...

  5. php纯原生实现数组二分法

    代码如下 $arr = [1,3,5,7,9];//$arr = range(1,10000);var_dump(find($arr, 2)); function find(array $arr, $ ...

  6. NSArray打印汉字的方法

    (1) NSArray打印汉字 通过重载NSArray的- (NSString *)descriptionWithLocale:(id)locale方法 方法体例如以下: //依据设置的locale ...

  7. 世纪佳缘信息爬取存储到mysql,下载图片到本地,从数据库选取账号对其发送消息更新发信状态

    利用这种方法,可以把所有会员信息存储下来,多线程发信息,10秒钟就可以对几百个会员完成发信了. 首先是筛选信息后爬取账号信息, #-*-coding:utf-8-*- import requests, ...

  8. 【java】 java 设计模式概述

    一.设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接 ...

  9. Windows之Xmanager连接linux打开Oracle视图操作

    前提:安装Xmanager 能够百度Xmanager下载其破解版或者带注冊机的版本号,也能够官网下载.只是须要秘钥(建议下载企业版) 官网下载地址:http://www.netsarang.com/d ...

  10. mybatis由浅入深day01_1课程安排_2对原生态jdbc程序中问题总结

    mybatis 第一天 mybatis的基础知识 1 课程安排: mybatis和springmvc通过订单商品 案例驱动 第一天:基础知识(重点,内容量多) 对原生态jdbc程序(单独使用jdbc开 ...