Codeforces 253D-Table with Letters - 2(有技巧的遍历)

题目链接

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

题意:就是给定一个平面,现在要在平面内找一些子平面,子平面要求是平面四个角的元素要相等,且子平面内字母‘a’出现的次数要不大于k个,现在求能找到多少个这样的子平面。

题解:因为数据比较小,所以直接暴力枚举就能过,上下每两排枚举,然后再枚举左右端点。

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=4e2+10;
const int mod=1e9+7;
const int inf=1e8;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define mid l+(r-l)/2
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI 3.14159265358979323846
int dir[4][2]= {0,-1,-1,0,0,1,1,0};
typedef long long ll;
using namespace std;
int main()
{
    char maps[maxn][maxn];
    int sum[maxn][maxn];
    int n,m,s;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    scanf("%d%d%d",&n,&m,&s);
    me(sum,0);
    for(int i=1; i<=n; i++)
    {
        scanf("%s",maps[i]+1);
        for(int j=1; j<=m; j++)
        {
            sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];///计算平面中a的个数
            if(maps[i][j]=='a')
                sum[i][j]++;
        }
    }
    int cnt[300];
    ll ans=0;
    for(int i=1; i<n; i++)
        for(int j=i+1; j<=n; j++)
        {
            int temp=1;me(cnt,0);
            for(int k=1; k<=m; k++)
            {
                if(maps[i][k]==maps[j][k])
                {
                    cnt[maps[i][k]]--;///减去它的左端点那一列。
                    while(temp<=m&&sum[j][temp]-sum[j][k-1]-sum[i-1][temp]+sum[i-1][k-1]<=s)
                    {
                        if(maps[i][temp]==maps[j][temp])
                            cnt[maps[j][temp]]++;///这样记录下来的话,只有第一次会遍历,后面temp都等于m,这里就不会再循环。
                        temp++;
                    }
                    if(cnt[maps[i][k]]>0)
                        ans+=cnt[maps[i][k]];
                }
            }

        }
    printf("%lld\n",ans);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值