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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值