CodeForces 253D Table with Letters - 2 减小复杂度的技巧+交替枚举
来源:互联网 发布:斯坦福大学 知乎 编辑:程序博客网 时间:2024/05/22 10:33
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 wroten 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 ton, and columns — from left to right with integers from 1 tom.
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 integersx1, y1, x2, y2 such that1 ≤ 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 holdsx1 ≤ 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^4的算法,codeforce上肯定超时
那么这题如何解决?要么是一个高效的数据结构来降低复杂度,要么就是技巧(枚举的技巧,标记的技巧等)
1,首先对于K的判断,求一个矩阵范围内的a的数量那就是递推啊,这个算做是基础吧,n^2解决
2,枚举的策略是枚举列。每次枚举两列,然后再这两行中,交替进行列的枚举,同时使用标记,找一个样例手推一遍代码就知道为什么了。
3,总的复杂度n^3还是很好的解决方案的,做过一遍这样的题,下一次就不慌了。
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int maxn = 500 ;int n,m,K;char mapp[maxn][maxn] ;int num[maxn][maxn] ;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",mapp[i]+1); } memset(num,0,sizeof(num)); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ num[i][j] = num[i-1][j]+num[i][j-1]-num[i-1][j-1] ; if(mapp[i][j]=='a')num[i][j]++; } }// for(int i=1;i<=n;i++){// for(int j=1;j<=m;j++){// printf("%d ",num[i][j]);// }printf("\n");// } long long ans = 0; int flag[500] ; for(int i=1;i<=n;i++){ for(int j = i+1;j<=n;j++){ int p = 1 ; memset(flag,0,sizeof(flag)); for(int k = 1;k<=m;k++){ if(mapp[i][k]!=mapp[j][k])continue ; flag[mapp[i][k]]--; while(p<=m&&num[j][p]-num[i-1][p]-num[j][k-1]+num[i-1][k-1]<=K){ if(mapp[i][p]==mapp[j][p])flag[mapp[i][p]]++; ; p++; } if(flag[mapp[i][k]]>0)ans+=flag[mapp[i][k]]; } } } printf("%I64d\n",ans); } return 0;}
- CodeForces 253D Table with Letters - 2 减小复杂度的技巧+交替枚举
- 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
- cf 154.div2 D. Table with Letters - 2
- 减小模运算时间复杂度的小技巧
- Codeforces 485D Maximum Value【思维+技巧枚举】
- Codeforces Round #253 (Div. 2)A. Anton and Letters
- Codeforces Round #253 (Div. 2) A. Anton and Letters
- codeforces D. Multiplication Table
- Codeforces 426D Sereja and Table【思维+暴力枚举】好题!
- Codeforces Round #436 (Div. 2) A.Fair Game B.Polycarp and Letters C. Bus D.Make a Permutation!
- Codeforces 617D Polyline 【枚举】
- CodeForces 245D Restoring Table
- CodeForces 245D Restoring Table
- Codeforces 448 D. Multiplication Table
- Codeforces 448D Multiplication Table
- Jersey框架的MVC
- KNN(一)--简单KNN原理及实现
- 一个比日本还要可怕的国家!
- 如何使app 直接关掉,不在进入后台
- Java知识图谱收集整理
- CodeForces 253D Table with Letters - 2 减小复杂度的技巧+交替枚举
- JVM学习-java内存区域与异常
- centos 安装twemproxy
- 线程安全
- 自己学习自定义view的一些总结
- Linux思维导图整理
- servlet genericservlet httpservlet
- Textview超链接
- DTD中的实体Entity