Codeforces 253D-Table with Letters
来源:互联网 发布:原创设计的衣服淘宝店 编辑:程序博客网 时间:2024/05/27 06:15
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 mcharacters 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.
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.
Print a single integer — the number of required subtables.
3 4 4aabbbaabbaab
2
4 5 1ababaccacaccacbcbabc
1
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).
题意:给出一个n行m列的矩阵,问有多少个子矩阵满足四个角相同,并且子矩阵内字符a的个数不大于k
解题思路:先做一下预处理,然后枚举,每次枚举两行,然后再这两行中,交替进行列的枚举,同时标记出现的字符
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;int n,m,k;char ch[500][500];int a[500][500];int main(){ freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); while(~scanf("%d %d %d",&n,&m,&k)) { for(int i=1;i<=n;i++) scanf("%s",ch[i]+1); memset(a,0,sizeof(a)); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { a[i][j]=a[i-1][j]+a[i][j-1]-a[i-1][j-1] ; if(ch[i][j]=='a') a[i][j]++; } } LL ans=0; int flag[500]; for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { int kk=1; memset(flag,0,sizeof(flag)); for(int p=1;p<=m;p++) { if(ch[i][p]!=ch[j][p]) continue ; flag[ch[i][p]]--; while(kk<=m&&a[j][kk]-a[i-1][kk]-a[j][p-1]+a[i-1][p-1]<=k) { if(ch[i][kk]==ch[j][kk]) flag[ch[i][kk]]++; kk++; } if(flag[ch[i][p]]>0) ans+=flag[ch[i][p]]; } } } printf("%lld\n",ans); } return 0;}
- CodeForces 253D Table with Letters - 2
- Codeforces 253D-Table with Letters
- CodeForces 253D Table with Letters
- 单调队列1004 Codeforces Round #154 (Div. 2) 253D. Table with Letters - 2
- CodeForces 253D Table with Letters - 2 减小复杂度的技巧+交替枚举
- cf 154.div2 D. Table with Letters - 2
- codeforces D. Multiplication Table
- CodeForces 245D Restoring Table
- CodeForces 245D Restoring Table
- Codeforces 448 D. Multiplication Table
- Codeforces 448D Multiplication Table
- Codeforces 448D Multiplication Table
- 【Codeforces】 226D The table
- Codeforces 448 D. Multiplication Table
- 【CodeForces】[448D]Multiplication Table
- CodeForces 448D Multiplication Table
- codeforces 338D GCD Table
- codeforces 338D GCD Table
- 软件开发项目流程
- 内存管理之memblock
- 4730: Alice和Bob又在玩游戏
- ObjectOutputStream和ObjectInputStream
- lua 运算符
- Codeforces 253D-Table with Letters
- 算法分析学习1
- KNN(一)--简单KNN原理及实现
- JAVA8 十大新特性详解
- 安卓SharedPreferences封装类,超好用
- 进程的内存映射
- 网页渲染流程
- 三个层面理解智能制造
- 笔记:Deep Robust Encoder Through Locality Preserving Low-Rank Dictionary