Table with Letters - 2(dp & 队列思想)

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.

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

因为要求一个平面内的a的个数,所以很容易想到利用前缀和求一下平面内的a的个数。剩下的工作,首先遍历平面的上下两行,两层循环,确定这个方阵的上下两个边。剩下的操作比较巧妙了,我们先确定左侧的边,之后想象有一条扫描线,从左向右扫描这个方阵,这条线与刚刚的上下两条边构成一条线段,这就是子阵的右边,我们只记录这条线段(要上下端点字符相同并且与左边构成矩阵中a的个数小于k的)的上方点的字符个数(因为上下字符是一样的,这样方便操作),这样的话,我们在遍历左边的边,遇到一个符合条件的(即,边的上下端点相同),那么就将这条边上端点字符的数量减一,因为我们在后面的扫描过程中知道在这条边右侧有多少个与这个相同的(两个端点与这条边两个端点相同的)边,但是当前这一条边和在这条边左边的边不能算上,所以我们要减一,这样我们就可以看看右边的数量,其实也就是当前边的上端点字符个数,加和就可以了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 4e2+5;
char character[maxn][maxn];
int sum[maxn][maxn];

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n, m, K;
    cin >> n >> m >> K;
    memset(sum, 0, sizeof(sum));
    for(int i = 1; i <= n; i++)
    {
        getchar();
        for(int j = 1; j <= m; j++)
        {
            character[i][j] = getchar();
            sum[i][j] = 0-sum[i-1][j-1]+sum[i-1][j]+sum[i][j-1];
            if(character[i][j] == 'a')    sum[i][j]++;
        }
    }
    long long ans = 0;
    int now[257];
    for(int i = 1; i <= n; i++)
        for(int j = i+1; j <= n; j++)
        {
            memset(now, 0, sizeof(now));
            for(int k = 1, p = 1; k <= m; k++)
            {
                if(character[i][k] != character[j][k])  continue ;
                now[character[i][k]]--;
                while(p <= m && sum[j][p]+sum[i-1][k-1]-sum[i-1][p]-sum[j][k-1] <= K)
                {
                    if(character[i][p] == character[j][p])  now[character[i][p]]++;
                    //printf("(%d, %d)~(%d, %d)\n", i, k, j, p);
                    //cout << "now[a]: " << now['a'] << ", " << "now[b]: " << now['b'] << endl;
                    p++;
                }
                if(now[character[i][k]] > 0)    ans += now[character[i][k]];
            }
        }
    cout << ans << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值