牛客第七场 Sudoku Subrectangles
链接:https://www.nowcoder.com/acm/contest/145/J
来源:牛客网
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.
输入例子:
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).
说明
For sample 2, the grid has 150 nonempty subrectangles, and all of them are sudoku-like. 预处理每一个点向下想右最大的延伸长度,然后再遍历一遍所有点,枚举所有点是否成立的情况
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e3+10;
const ll mod = 1e9+7;
const double pi = acos(-1.0);
const double eps = 1e-8;
ll n, m, lmax[maxn][maxn], pos[maxn];
ll umax[maxn][maxn], len[maxn];
char s[maxn][maxn];
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
scanf("%lld%lld",&n,&m);
for( ll i = 1; i <= n; i ++ ) {
scanf("%s",s[i]+1);
}
for( ll i = 1; i <= n; i ++ ) {
memset(pos,0,sizeof(pos));
for( ll j = 1; j <= m; j ++ ) {
lmax[i][j] = min(lmax[i][j-1]+1,j-pos[s[i][j]]);
pos[s[i][j]] = j;
}
}
for( ll j = 1; j <= m; j ++ ) {
memset(pos,0,sizeof(pos));
for( ll i = 1; i <= n; i ++ ) {
umax[i][j] = min(umax[i-1][j]+1,i-pos[s[i][j]]);
pos[s[i][j]] = i;
}
}
ll ans = 0;
for( ll r = 1; r <= m; r ++ ) {
memset(len,0,sizeof(len));
for( ll d = 1; d <= n; d ++ ) {
for( ll i = 0; i < lmax[d][r]; i ++ ) {
len[i] = min(umax[d][r-i],len[i]+1);
if(i) {
len[i] = min(len[i],len[i-1]);
}
ans += len[i];
}
for( ll i = lmax[d][r]; i <= 54; i ++ ) {
len[i] = 0;
}
}
}
printf("%lld\n",ans);
return 0;
}
牛客第七场 Sudoku Subrectangles的更多相关文章
- 牛客第七场 Minimum Cost Perfect Matching 规律
题意:1-n-1个数和1-n-1个数两两匹配,每次匹配将两个数的值进行与运算,要求每次匹配与运算的和相加最小,问满足匹配的配对方式 分析:打表前10个数与运算最小的匹配,我们发现,从n-1开始按位取反 ...
- 利用数目找中位数(牛客第七场E)
https://ac.nowcoder.com/acm/contest/887/E 树状数组做法(代码有注释) #include<bits/stdc++.h> using namespac ...
- 2019牛客第八场多校 E_Explorer 可撤销并查集(栈)+线段树
目录 题意: 分析: @(2019牛客暑期多校训练营(第八场)E_Explorer) 题意: 链接 题目类似:CF366D,Gym101652T 本题给你\(n(100000)\)个点\(m(1000 ...
- 牛客网第二场Jfarm(随机化+二维前缀和)
链接:https://www.nowcoder.com/acm/contest/140/J 来源:牛客网 White Rabbit has a rectangular farmland of n*m. ...
- 牛客网第一场E题 Removal
链接:https://www.nowcoder.com/acm/contest/139/E 来源:牛客网 Bobo has a sequence of integers s1, s2, ..., sn ...
- 牛客网第一场 A Monotonic Matrix
链接:https://www.nowcoder.com/acm/contest/139/A来源:牛客网 Count the number of n x m matrices A satisfying ...
- 牛客第三场多校 E Sort String
链接:https://www.nowcoder.com/acm/contest/141/E来源:牛客网 Eddy likes to play with string which is a sequen ...
- 牛客第三场多校 H Diff-prime Pairs
链接:https://www.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy has solved lots of problem involving calcul ...
- 牛客第五场多校 J plan 思维
链接:https://www.nowcoder.com/acm/contest/143/J来源:牛客网 There are n students going to travel. And hotel ...
随机推荐
- vue 移动端/PC常见问题及解决方法
一.判断手机/PC浏览器语言 navigator.language // 返回语言代码 语言代码文档: http://www.lingoes.cn/zh/translator/langcode.htm ...
- ubuntu 13.10 eclipse 菜单栏不可用的问题
最近手贱,从官网上下载了最新的版eclipse,把一原来的3.8版本替换了,然后就发现eclipse菜单栏废了,吓我一跳,以为Ubuntu又初问题,又重新弄了一次eclipse(我直接用的压缩包),上 ...
- 从windows平台转战ubuntu
说到ubuntu,可能很多人会有些陌生,但对于有些人很熟悉.ubuntu是linux里面最为流行的一版,以下来自百度百科. Ubuntu(乌班图)是基于Debian GNU/Linux,支 ...
- 自己动手写Spring框架--IOC、MVC
对于一名Java开发人员,我相信没有人不知道 Spring 框架,而且也能够轻松就说出 Spring 的特性-- IOC.MVC.AOP.ORM(batis). 下面我想简单介绍一下我写的轻量级的 S ...
- Golang高效实践之array、slice、map
前言 Golang的slice类型为连续同类型数据提供了一个方便并且高效的实现方式.slice的实现是基于array,slice和map一样是类似于指针语义,传递slice和map并不涉及底层数据结构 ...
- JVM系列(1)- JVM常见参数及堆内存分配
常见参数配置 基于JDK1.6 -XX:+PrintGC 每次触发GC的时候打印相关日志 -XX:+UseSerialGC 串行回收模式 -XX:+PrintGCDetails 打印更详细的GC日志 ...
- 2、JAVA相关基础的学习和工具
个人感觉,各种语言的基础知识,例如标识符,运算符等在宏观上几乎是一样的,只是在某些方面上会有一点点差异,因为本人已经有了语言基础,所以对于标识符,关键字,运算符等方面的只是便不作赘述,敬请谅解,如果你 ...
- Laya 中缩放的实现
Laya 缩放功能的实现 在 laya 中实现滚轮对选中对象的缩放,涉及到以下两个模块: 事件 容器坐标 1. 事件 在 Laya 中, Event 是事件类型的集合.包含了常见的鼠标事件.键盘事件. ...
- Kafka 系列(四)—— Kafka 消费者详解
一.消费者和消费者群组 在 Kafka 中,消费者通常是消费者群组的一部分,多个消费者群组共同读取同一个主题时,彼此之间互不影响.Kafka 之所以要引入消费者群组这个概念是因为 Kafka 消费者经 ...
- F#周报2019年第33期
新闻 宣告.NET Core 3.0预览版8 新的fable.io站点伴随着更多文档发布 正在努力使你的团队相信F#的益处?Compositional IT能够提供帮助 提名2019年度F#社区英雄 ...