本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!