Table with Letters-2(CF-253D)

Problem 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 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 列的矩阵,问有多少子矩阵满足四个角相同而且子矩阵中字符 a 的个数不大于 k

思路:子矩阵字符 a 个数不大于 k 十分容易计算,二维前缀和即可解决,难点在于要求四个角相同,设一个数组 num 用于存储当前 left 和 right 之间有上下边界相同字母 a 的对数,以 (i,left) 为基准进行比较,最后只需要累加 num[a[i][left]] 的个数即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;

int n,m,k;
char a[N][N];
int sum[N][N];
int num[N];
void init(){//计算前缀和,统计a个数
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            if(a[i][j]=='a')
                sum[i][j]++;
            sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
        }
    }

}
int query(int x1,int y1,int x2,int y2) { //查询
    return sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1];
}
int main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1; i<=n; i++)
        scanf("%s",a[i]+1);
    init();

    LL res=0;
    for(int i=1; i<=n; i++) {//子矩阵上边界,从第i行起
        for(int j=i+1; j<=n; j++) {//子矩阵下边界,到第j行为止
            int right=1;//子矩阵右边界,从第1列起
            memset(num,0,sizeof(num));//当前left和right之间有上下边界相同字母a的对数
            for(int left=1; left<=m; left++) {//子矩阵左边界,从第left列起
                if(a[i][left]==a[j][left]){//左上与右下相同
                    num[a[i][left]]--;
                    int cnt=query(i,left,j,right);//查询子矩阵a的个数
                    while(right<=m&&cnt<=k){
                        if(a[i][right]==a[j][right])
                            num[a[i][right]]++;
                        right++;
                        cnt=query(i,left,j,right);
                    }
                    if(num[a[i][left]]>0)//累加
                        res+=num[a[i][left]];
                }
            }
        }
    }
    printf("%lld\n",res);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值