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 mcharacters 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 holdsx1 ≤ 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.

Sample test(s)
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的列的最小值,每次遇到两个顶点相同的列,该字符数量加1

#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
int a[410][410], cnt[130];
char map[410][410];
long long ans;

int main() {
    int m, n, i, j, sum, k, s, e, temp;
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    scanf("%d%d%d", &m, &n, &k);
    for (i = 1; i <= m; ++i)
        scanf("%s", map[i] + 1);
    for (j = 1; j <= n; ++j) {
        for (i = 1; i <= m; ++i) {
            a[j][i] = a[j][i - 1]+(map[i][j] == 'a');
            //    printf("j=%d i=%d %d\n",i,j,a[j][i]);
        }
    }
    for (s = 1; s < m; ++s) {
        for (e = s + 1; e <= m; ++e) {
            //   printf("s=%d e=%d\n",s,e);
            sum = 0;
            temp = 1;
            memset(cnt, 0, sizeof (cnt));
            for (j = 1; j <= n; ++j) {
                if (map[s][j] == map[e][j])
                    ++cnt[map[s][j]];
                sum += a[j][e] - a[j][s - 1];
                while (sum > k) {
                    sum -= a[temp][e] - a[temp][s - 1];
                    if (map[s][temp] == map[e][temp])
                        --cnt[map[s][temp]];
                    ++temp;
                }
                if (map[s][j] == map[e][j] && cnt[map[s][j]] > 1) {
                    ans += cnt[map[s][j]] - 1;
                    //  printf("%c %d\n",map[s][j],ans);
                }
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}
这条路我们走的太匆忙~拥抱着并不真实的欲望~
原文地址:https://www.cnblogs.com/baidongtan/p/2809444.html