牛客第七场 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 ...
随机推荐
- NLP(十三)中文分词工具的使用尝试
本文将对三种中文分词工具进行使用尝试,这三种工具分别为哈工大的LTP,结巴分词以及北大的pkuseg. 首先我们先准备好环境,即需要安装三个模块:pyltp, jieba, pkuseg以及L ...
- Asp.Net Core 发布到 Docker(Linux Centos 虚拟机,使用Dockerfile)
实践一下 Asp.Net Core (基于.net core 2.2)部署到Docker 一.准备工作: 1. 使用Virtualbox创建一个Centos系统的虚拟机,并安装docker和vim 2 ...
- (通俗易懂小白入门)网络流最大流——EK算法
网络流 网络流是模仿水流解决生活中类似问题的一种方法策略,来看这么一个问题,有一个自来水厂S,它要向目标T提供水量,从S出发有不确定数量和方向的水管,它可能直接到达T或者经过更多的节点的中转,目前确定 ...
- Extjs4 combobox autoLoad: true 时,加载两次
问题是这样的,combobox 远程加载数据时,当我们把 store 设置为 autoLoad: tue, 时,这样页面加载时,store 会load 一次,但是我们在第一次点击 下来框时,他还会 ...
- restapi(4)- rest-mongo : MongoDB数据库前端的httpserver
完成了一套标准的rest风格数据库CRUD操作httpserver后发现有许多不足.主要是为了追求“通用”两个字,想把所有服务接口做的更“范generic”些,结果反而限制了目标数据库的特点,最终产生 ...
- 对于微信UnionID在公众平台以及小程序里面的获取
首先介绍下UnionID的作用,在注册了微信开放平台(注意,这里是开放平台,不是微信公众平台)之后,同一个微信号在这个开放平台下的项目上面的UnionID都是统一的,通俗的说就是,小程序跟公众号项目在 ...
- 【POJ - 3259】Wormholes(最短路 Floyd算法)
Wormholes 题目描述 教学楼里有很多教室,这些教室由双向走廊连接.另外,还存在一些单向的秘密通道,通过它们可以回到过去.现在有 N (1 ≤ N ≤ 500) 个教室,编号 1..N, M ( ...
- 洛谷 P1903 [国家集训队]数颜色
题意简述 给定一个数列,支持两个操作 1.询问l~r有多少不同数字 2.修改某个数字 题解思路 带修莫队 如果修改多了,撤销修改 如果修改少了,进行修改 代码 #include <cmath&g ...
- (十四)c#Winform自定义控件-键盘(一)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- Windows Server2008 监控服务器性能
下面学习Windows Server2008监控服务器性能包括 日志管理,归档日志,日志排错,怎么配置计算机以转发和收集事件,使用任务管理监控内存和CPU的使用,查看程序内存使用,查看程序CPU使用, ...