链接:https://www.nowcoder.com/acm/contest/145/J
来源:牛客网

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.

输入例子:
2 3
AaA
caa
输出例子:
11

-->

示例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 (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).
示例2

输入

复制

4 5
abcde
fGhij
klmno
pqrst

输出

复制

150

说明

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的更多相关文章

  1. 牛客第七场 Minimum Cost Perfect Matching 规律

    题意:1-n-1个数和1-n-1个数两两匹配,每次匹配将两个数的值进行与运算,要求每次匹配与运算的和相加最小,问满足匹配的配对方式 分析:打表前10个数与运算最小的匹配,我们发现,从n-1开始按位取反 ...

  2. 利用数目找中位数(牛客第七场E)

    https://ac.nowcoder.com/acm/contest/887/E 树状数组做法(代码有注释) #include<bits/stdc++.h> using namespac ...

  3. 2019牛客第八场多校 E_Explorer 可撤销并查集(栈)+线段树

    目录 题意: 分析: @(2019牛客暑期多校训练营(第八场)E_Explorer) 题意: 链接 题目类似:CF366D,Gym101652T 本题给你\(n(100000)\)个点\(m(1000 ...

  4. 牛客网第二场Jfarm(随机化+二维前缀和)

    链接:https://www.nowcoder.com/acm/contest/140/J 来源:牛客网 White Rabbit has a rectangular farmland of n*m. ...

  5. 牛客网第一场E题 Removal

    链接:https://www.nowcoder.com/acm/contest/139/E 来源:牛客网 Bobo has a sequence of integers s1, s2, ..., sn ...

  6. 牛客网第一场 A Monotonic Matrix

    链接:https://www.nowcoder.com/acm/contest/139/A来源:牛客网 Count the number of n x m matrices A satisfying ...

  7. 牛客第三场多校 E Sort String

    链接:https://www.nowcoder.com/acm/contest/141/E来源:牛客网 Eddy likes to play with string which is a sequen ...

  8. 牛客第三场多校 H Diff-prime Pairs

    链接:https://www.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy has solved lots of problem involving calcul ...

  9. 牛客第五场多校 J plan 思维

    链接:https://www.nowcoder.com/acm/contest/143/J来源:牛客网 There are n students going to travel. And hotel ...

随机推荐

  1. 林大妈的JavaScript基础知识(三):JavaScript编程(3)原型

    在一般的编程语言中,我们使用继承来复用代码,做成良好的数据结构.而在JavaScript中,我们使用原型来实现以上的需求.由于JavaScript专注于对象而摒弃了类,我们要明白原型和继承的确是有差异 ...

  2. Sring 的 @AliasFor 使用规则

    一.该标签存在的意义 顾名思义 @AliasFor 表示别名,它可以注解到自定义注解的两个属性上,表示这两个互为别名,也就是说这两个属性其实同一个含义.该标签存在的含义,从网上查发现有个点, 若  自 ...

  3. JAVA开始(基础篇)

    数据类型 Boolean        1位Byte              1个字节(8位)Short             2个字节Char              2个字节Int      ...

  4. win10+Anaconda3+CUDA9.0+CUDNN7.1+TensorFlow-gpu1.9+Pycharm

    想在win10上运行下YOLO的例子,要先配置环境,折腾了两天,终于好了,整理下自己觉得有用且正确的流程. win10+Anaconda3+CUDA9.0+CUDNN7.1+TensorFlow1.9 ...

  5. 配置Windows Server 2008环境

    上一章已经把Windows Server2008操作系统安装完毕,接下来配置一下Windows Server环境.配置网络和共享中心.配置桌面环境.配置用户IE设置.安装Telnet远程工具.配置文件 ...

  6. 颜色下拉菜单(combox)

    using System; using System.Drawing; using System.Collections; using System.ComponentModel; using Sys ...

  7. GRPC快速入门

    转载请注明来自ChenJiehua的<GRPC快速入门> GRPC是一个高性能.通用的开源RPC框架,基于HTTP/2协议标准和Protobuf序列化协议开发,支持众多的开发语言. 概述 ...

  8. Zabbix-绘制动态拓扑图高级篇

    0.官网文档介绍: https://www.zabbix.com/documentation/4.0/manual/config/visualisation/maps/map 一.设备名字使用宏显示 ...

  9. java 计算器

    初识java:利用swing制作一个简单的计算器,仿造window10内置计算器标准模式下的界面. 涉及学习内容: 设置窗口 设计界面按键 设置文本框:只读 String字符串操作:与double类型 ...

  10. Cassandra查询操作趟坑记录

    例子表 CREATE TABLE employee ( name TEXT, age SMALLINT, phone TEXT, bornDate DATE, createDate timestamp ...