CodeForces - 253D:Table with Letters - 2(矩阵前缀和)

Discription
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

3 4 4
aabb
baab
baab

Output

2

Input

4 5 1
ababa
ccaca
ccacb
cbabc

Output

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).

题意
给定一个矩形的n×m表,表中的每个单元格都包含一些英文字母。让我们用从1到n的整数从上到下对表格行进行编号,并使用从左到右的整数从1到m的整数。

之后,Vasya看着生成的矩形表,想知道有多少个子表符合以下两个条件:

子表最多包含带有“a”字母的k个单元格;
位于子表的所有四个角单元中的所有字母都是相等的。
形式上,子表的定义如下。它由四个整数x1,y1,x2,y2定义,使得1≤x1<x2≤n,1≤y1<y2≤m。然后子表包含所有这样的单元格(x,y)(x是行号,y是列号),对于这些单元格,下面的不等式保持x1≤x≤x2,y1≤y≤y2。表格的角单元格是单元格(x1,y1),(x1,y2),(x2,y1),(x2,y2)。

思路
通过求矩阵的前缀和枚举求解。
详见代码。

AC代码

#include<bits/stdc++.h>
const int maxn=1e5+10;
using namespace std;
int n,m,kk;
char a[500][500];
int num[500][500];///num[i][j]代表从(1,1)到第(i,j)位置的矩阵的前缀和
map<char,int>q;///选取矩阵中各个字母的出现的对数
int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    scanf("%d%d%d",&n,&m,&kk);
    for(int i=1; i<=n; i++)
        scanf("%s",a[i]+1);
    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(a[i][j]=='a') num[i][j]++;
        }
    long long ans=0;
    for(int i=1; i<=n; i++)
    {
        for(int j=i+1; j<=n; j++) ///枚举矩阵的上边i和下边j
        {
            int p=1;///代表矩阵右边
            q.clear();
            for(int k=1; k<=m; k++) ///矩阵左边
            {
                if(a[i][k]!=a[j][k]) continue;///不满足矩形四个点相同
                while(p<=m&&num[j][p]-num[i-1][p]-num[j][k-1]+num[i-1][k-1]<=kk)
                {
                    if(a[i][p]==a[j][p]) q[a[i][p]]++;
                    p++;///矩形满足a个数小于kk的要求,矩阵右边增大,直到不满足,那么增大左边k++
                }
                q[a[i][k]]--;///减去重复加的它本身
                if(q[a[i][k]]>0) ans+=q[a[i][k]];///因为矩形左边固定,看右边满足四个点相同条件下有多少个这样的矩形
                //cout<<q[a[i][k]]<<endl;
            }
        }
    }
    printf("%I64d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值