CF253D Table with Letters - 2 优美枚举

2023-11-10 02:38

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

题目描述

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 nn lines containing mm characters each. Thus, he got a rectangular n×mn×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 nn , and columns — from left to right with integers from 1 to mm .

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 kk 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 x_{1},y_{1},x_{2},y_{2}x1​,y1​,x2​,y2​ such that 1<=x_{1}<x_{2}<=n , 1<=y_{1}<y_{2}<=m . Then the subtable contains all such cells (x,y)(x,y) ( xx is the row number, yy is the column number), for which the following inequality holds x_{1}<=x<=x_{2},y_{1}<=y<=y_{2}x1​<=x<=x2​,y1​<=y<=y2​ . The corner cells of the table are cells (x_{1},y_{1})(x1​,y1​) , (x_{1},y_{2})(x1​,y2​) , (x_{2},y_{1})(x2​,y1​) , (x_{2},y_{2})(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.

输入格式

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

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

输出格式

Print a single integer — the number of required subtables.

输入输出样例

输入 #1复制

3 4 4
aabb
baab
baab

输出 #1复制

2

输入 #2复制

4 5 1
ababa
ccaca
ccacb
cbabc

输出 #2复制

1

说明/提示

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


题意:

给你n*m的矩阵,统计满足一个矩阵四个角的字母相同,且满足a的个数<=k的子矩阵个数。

分析:

神级的优化枚举,固定枚举上行i和下行j,枚举左列和右列需要一定的技巧,我们依旧枚举从1到n枚举左列l,右列r从1开始,寻找可以满足的子矩阵,如果符合则就用num数组记录下来,如果不满足则r不变,l往下继续,注意要当前列消除num。

 

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+10;
char a[500][500];
int sum[500][500];//sum[i][j]代表 宽为i 长为j的矩阵中a的数量
int main()
{//freopen("input.txt", "r", stdin);//freopen("output.txt", "w", stdout);int n,m,k;scanf("%d%d%d",&n,&m,&k);for(int i=1; i<=n; i++)scanf("%s",a[i]+1);memset(sum,0,sizeof(sum));for(int i=1; i<=n; i++)for(int j=1; j<=m; j++){sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];if(a[i][j]=='a')sum[i][j]++;}ll ans=0;int num[300];//选取矩阵中各个字母的出现的对数for(int i=1; i<=n; i++){for(int j=i+1; j<=n; j++) //枚举矩阵的上边i和下边j{int r=1;//代表矩阵右边memset(num,0,sizeof(num));for(int l=1; l<=m; l++) //矩阵左边{if(a[i][l]!=a[j][l])continue;//不满足矩形四个点相同num[a[i][l]]--;while(r<=m&&sum[j][r]-sum[i-1][r]-sum[j][l-1]+sum[i-1][l-1]<=k){if(a[i][r]==a[j][r])num[a[i][r]]++;r++;//矩形满足a个数小于kk的要求,矩阵右边增大,直到不满足,那么增大左边k++}if(num[a[i][l]]>0)ans+=num[a[i][l]];//因为矩形左边固定,看右边满足四个点相同条件下有多少个这样的矩形}}}printf("%lld\n",ans);}
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=505;
int a[N][N];
char mp[N][N];
int num[400],sum[N][N];
int n,m,k; int get(int x1, int y1, int x2, int y2)
{return sum[x1][y1] - sum[x1][y2 - 1] - sum[x2 - 1][y1] + sum[x2 - 1][y2 - 1];
}
int init()
{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] + a[i][j];}}
}
int main()
{freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);scanf("%d%d%d",&n,&m,&k);for(int i=1; i<=n; i++){scanf("%s",mp[i]+1);for(int j=1; j<=m; j++){if(mp[i][j]=='a')a[i][j]=1;}}init();LL ans=0;for(int i=1; i<=n; i++){for(int j=i+1; j<=n; j++){int r=1;memset(num,0,sizeof(num));for(int l=1; l<=m; l++){if(mp[i][l]!=mp[j][l]){continue;}num[mp[i][l]]--;while(r<=m&&get(j,r,i,l)<=k){if(mp[i][r]==mp[j][r]){num[mp[i][r]]++;}r++;}if(num[mp[i][l]]>0)ans+=num[mp[i][l]];}}}printf("%lld",ans);//cout<<ans<<endl;return 0;
}

 

这篇关于CF253D Table with Letters - 2 优美枚举的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

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示例总结报错原

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

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

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

Java 枚举的基本使用方法及实际使用场景

《Java枚举的基本使用方法及实际使用场景》枚举是Java中一种特殊的类,用于定义一组固定的常量,枚举类型提供了更好的类型安全性和可读性,适用于需要定义一组有限且固定的值的场景,本文给大家介绍Jav... 目录一、什么是枚举?二、枚举的基本使用方法定义枚举三、实际使用场景代替常量状态机四、更多用法1.实现接

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. 查看透视表总结前言

C 语言中enum枚举的定义和使用小结

《C语言中enum枚举的定义和使用小结》在C语言里,enum(枚举)是一种用户自定义的数据类型,它能够让你创建一组具名的整数常量,下面我会从定义、使用、特性等方面详细介绍enum,感兴趣的朋友一起看... 目录1、引言2、基本定义3、定义枚举变量4、自定义枚举常量的值5、枚举与switch语句结合使用6、枚

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co