codeforces 253D. Table with Letters - 2

2024-09-04 23:18
文章标签 codeforces table letters 253d

本文主要是介绍codeforces 253D. Table with Letters - 2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目

D. Table with Letters - 2

time limit per test

2 seconds

memory limit per test

256 megabytes

input

input.txt

output

output.txt

Vasya has recently started to learn English. Now he needs to remember how to write English letters. He isn't sure about some of them, so he decided to train a little.

He found a sheet of squared paper and began writing arbitrary English letters there. In the end Vasya wrote n lines containing m characters each. Thus, he got a rectangular n × m table, each cell of the table contained some English letter. Let's number the table rows from top to bottom with integers from 1 to n, and columns — from left to right with integers from 1 to m.

After that Vasya looked at the resulting rectangular table and wondered, how many subtables are there, that matches both following conditions:

  • the subtable contains at most k cells with "a" letter;
  • all letters, located in all four corner cells of the subtable, are equal.

Formally, a subtable's definition is as follows. It is defined by four integers x1, y1, x2, y2 such that 1 ≤ x1 < x2 ≤ n, 1 ≤ y1 < y2 ≤ m. Then the subtable contains all such cells (x, y) (x is the row number, y is the column number), for which the following inequality holds x1 ≤ x ≤ x2, y1 ≤ y ≤ y2. The corner cells of the table are cells (x1, y1), (x1, y2), (x2, y1), (x2, y2).

Vasya is already too tired after he's been writing letters to a piece of paper. That's why he asks you to count the value he is interested in.

Input

The first line contains three integers n, m, k (2 ≤ n, m ≤ 400; 0 ≤ k ≤ n·m).

Next n lines contain m characters each — the given table. Each character of the table is a lowercase English letter.

Output

Print a single integer — the number of required subtables.

Examples

input

Copy

3 4 4
aabb
baab
baab

output

Copy

2

input

Copy

4 5 1
ababa
ccaca
ccacb
cbabc

output

Copy

1

Note

There are two suitable subtables in the first sample: the first one's upper left corner is cell (2, 2) and lower right corner is cell (3, 3), the second one's upper left corner is cell (2, 1) and lower right corner is cell (3, 4).

思路:先通过二维前缀和求出 每个点到左上角的矩形中一共有多少个a,然后判断任意一个矩形的时候类似容斥定理加加减减就好。关键是端点的枚举方式,首先固定上下的行然后固定左端点枚举右端点,很巧妙的是。用一个数组来记录上下这两行中取同一列的时候字母相同的列数用字母对应的acsii码做下标(先不管配对的问题只要符合同一列字母相同就给他计算上)每出现一对就给他加一。这样在右端点循环完之后左端点再变大的时候,之前已经计算好的矩形中的a的个数本来就小于K现在左端点还右移了肯定更加符合K,这样子r就可以保证最少遍历一遍也就是说区间左端点右移就不用在初始化右端点了。

需要注意的是开long long和在统计列数的时候不要忘记把自身减去。

#include <iostream>
#include <cmath>
#include <string.h>
#include <algorithm>
#include <map>
#include <queue>
typedef  long long ll;
using namespace std;
int a[20000],sum[2000][2000];
char ha[1950][1950];
int main()
{freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);ll n,m,k,su=0;cin>>n>>m>>k;for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)cin>>ha[i][j];for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){sum[i][j]=sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1];if(ha[i][j]=='a')sum[i][j]++;// cout<<sum[i][j]<<endl;}}for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){int r=1;memset(a,0,sizeof(a));for(int l=1;l<=m;l++){if(ha[i][l]!=ha[j][l])continue;a[ha[i][l]]--;while(r<=m&&sum[j][r]-sum[j][l-1]-sum[i-1][r]+sum[i-1][l-1]<=k){if(ha[i][r]==ha[j][r])a[ha[i][r]]++;r++;//cout<<a[ha[i][r]]<<endl;}if(a[ha[i][l]]>0)su+=a[ha[i][l]];}}}cout<<su<<endl;
}

 

这篇关于codeforces 253D. Table with Letters - 2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1137313

相关文章

MySQL CTE (Common Table Expressions)示例全解析

《MySQLCTE(CommonTableExpressions)示例全解析》MySQL8.0引入CTE,支持递归查询,可创建临时命名结果集,提升复杂查询的可读性与维护性,适用于层次结构数据处... 目录基本语法CTE 主要特点非递归 CTE简单 CTE 示例多 CTE 示例递归 CTE基本递归 CTE 结

MySQL 8 中的一个强大功能 JSON_TABLE示例详解

《MySQL8中的一个强大功能JSON_TABLE示例详解》JSON_TABLE是MySQL8中引入的一个强大功能,它允许用户将JSON数据转换为关系表格式,从而可以更方便地在SQL查询中处理J... 目录基本语法示例示例查询解释应用场景不适用场景1. ‌jsON 数据结构过于复杂或动态变化‌2. ‌性能要

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

MySQL的ALTER TABLE命令的使用解读

《MySQL的ALTERTABLE命令的使用解读》:本文主要介绍MySQL的ALTERTABLE命令的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、查看所建表的编China编程码格式2、修改表的编码格式3、修改列队数据类型4、添加列5、修改列的位置5.1、把列

Pandas透视表(Pivot Table)的具体使用

《Pandas透视表(PivotTable)的具体使用》透视表用于在数据分析和处理过程中进行数据重塑和汇总,本文就来介绍一下Pandas透视表(PivotTable)的具体使用,感兴趣的可以了解一下... 目录前言什么是透视表?使用步骤1. 引入必要的库2. 读取数据3. 创建透视表4. 查看透视表总结前言

GORM中Model和Table的区别及使用

《GORM中Model和Table的区别及使用》Model和Table是两种与数据库表交互的核心方法,但它们的用途和行为存在著差异,本文主要介绍了GORM中Model和Table的区别及使用,具有一... 目录1. Model 的作用与特点1.1 核心用途1.2 行为特点1.3 示例China编程代码2. Tab

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op