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
baabOutput
2
Input
4 5 1
ababa
ccaca
ccacb
cbabcOutput
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;
}