牛客第七场 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 ...
随机推荐
- spring cloud eureka + feign,api远程调用
网上教程不少,有些就是复制粘贴,不结合实际生产. eureka不再阐述. 一般正常开发会有多个工程,且多个module. 我的习惯是: eureka server.权限.config.gateway ...
- 搭建nexus私服
一.安装 1.从网上下载nexus软件https://www.sonatype.com/download-oss-sonatype 下载Nexus Repository Manager OSS软件包 ...
- 启动Eclipse提示找不到虚拟机
由于硬盘坏了,把所有东西都清光了,今天重新安装Eclipse,出现了一点小插曲 安装的时候出现了这个画面,以前安装也是照着[软件安装管家]的发布装的,幸好还懂得几个英文单词,看了一下提示信息,直译:[ ...
- MariaDB 修改存储路径后启动失败问题解决
修改 MariaDB 路径到 home 路径下, 执行 systemctl start mariadb 启动MariaDB 时,报错提示: Job for mariadb.service failed ...
- 【精选】Markdown 语法汇总
博客园也能Markdown?美滋滋,Markdown真的是好用QAQ. 本文档按照Markdown各种常用语法类别,以文字描述+演示的方式来展现markdown语法的使用.Markdown 的目标是实 ...
- Linux curl 常用示例
本篇文章包含了curl的常用案例使用. 如果想了解curl选项的详细说明,请参考前一篇文章「Linux curl 命令详解」. 常见网页访问示例 基本用法 访问一个网页 curl https://ww ...
- [转载]windows下mongodb安装与使用整理
windows下mongodb安装与使用整理 一.首先安装mongodb 1.下载地址:http://www.mongodb.org/downloads 2.解压缩到自己想要安装的目录,比如d:\mo ...
- java8(二)方法引用
方法引用让你可以重复使用现有的方法定义,并像 Lambda 一样进行传递. 方法引用可以被看作仅仅调用特定方法的 Lambda 的一种快捷写法. 事实上,方法引用就是让你根据已有的方法实现来创建 La ...
- 洛谷 P3628 [APIO2010]特别行动队
题意简述 将n个士兵分为若干组,每组连续,编号为i的士兵战斗力为xi 若i~j士兵为一组,该组初始战斗力为\( s = \sum\limits_{k = i}^{j}xk \),实际战斗力\(a * ...
- 十分钟搞懂Lombok使用与原理
1 简介 Lombok是一款好用顺手的工具,就像Google Guava一样,在此予以强烈推荐,每一个Java工程师都应该使用它.Lombok是一种Java™实用工具,可用来帮助开发人员消除Java的 ...