CodeForces 253D Table with Letters - 2
来源:互联网 发布:台湾士林夜市 知乎 编辑:程序博客网 时间:2024/05/17 05:05
Description
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.
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.
Sample Input
3 4 4aabbbaabbaab
2
4 5 1ababaccacaccacbcbabc
1
Hint
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。
题解:预处理a的个数,储存字符相同的位置,暴力扫。
#include<stdio.h>#include<queue>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<assert.h>#include<stdlib.h>#include<time.h>#include<stack>#include<vector>#include<map>#include<set>#include<fstream>#define pi acos(-1.0)#define INF 0x3f3f3f3f#define debug printf("---------------\n");using namespace std;int main(){ freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); int n,m,k; char s[405][405]; int numa[405][405]; int cnt[30]; int event[30][405]; scanf("%d%d%d",&n,&m,&k); for(int i=0;i<n;i++) { scanf("%s",s[i]); } long long res=0; memset(numa,0,sizeof(numa)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { int have=0; if(s[i][j]=='a') { have=1; } numa[i+1][j+1]=numa[i][j+1]+numa[i+1][j]-numa[i][j]+have; } } for(int i=0;i<m;i++) { for(int j=i+1;j<m;j++) { memset(cnt,0,sizeof(cnt)); for(int y=0;y<n;y++) { if(s[y][i]==s[y][j]) { event[s[y][i]-'a'][cnt[s[y][i]-'a']++]=y; } } for(int c=0;c<26;c++) { int y2=0; for(int y=0;y<cnt[c];y++) { while(y2<cnt[c]&&(numa[event[c][y2]+1][j+1]-numa[event[c][y2]+1][i]-numa[event[c][y]][j+1]+numa[event[c][y]][i]<=k)) { ++y2; } y2=max(y2,y+1); res+=y2-y-1; } } } } printf("%I64d\n",res); 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 Round #253 (Div. 2)A. Anton and Letters
- Codeforces Round #253 (Div. 2) A. Anton and Letters
- codeforces D. Multiplication Table
- Codeforces Round #436 (Div. 2) A.Fair Game B.Polycarp and Letters C. Bus D.Make a Permutation!
- 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
- iOS使用xib自定义UIView
- javascript性能优化
- com.alibaba.fastjson.JSONObject.getString耗时问题
- 源代码解析Servlet生命周期 转自segmentfault
- HDU 4337 暴力
- CodeForces 253D Table with Letters - 2
- 1059. Prime Factors
- ui中各种手势的用法
- Unity3D技术之分析器 (Profiler) 窗口详解
- Multipart/form-data POST文件上传详解
- java对象转型
- c#——标准Dispose模式的实现
- 负载均衡学习笔记
- VARCHAR2(N CHAR)与VARCHAR2(N)的区别[Oracle基础]