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;
}
时间限制:1000ms 内存限制:512MB 输入文件名:s.in 输出文件名:s.out 题目描述: 给定一个长度为 N 的只含小写字母的字符串 S,每次操作可以选择一个位置 1 &le; x &le; N 和一个小写字母 c,接着把 Sₓ 改为 c。 你需要操作若干次,使得操作结束后,每相邻 K 个字符都不同,求最少的操作次数。保证 1 &le; K &le; 13。 输入格式: 第一行一个整数 T,表示测试数据组数。 接下来 T 组测试数据,对于每组测试数据: 第一行两个整数 N, K,分别表示字符串长度和子串的长度。 第二行一个长度为 N 的字符串 S。 输出格式: 对于每组测试数据,输出一行一个整数表示最少的操作次数。 样例输入 1(input1): 5 6 3 abaaba 5 2 hooch 8 3 cherykid 9 3 abbabbaba 9 3 aabbaabba 样例输出 1(output1): 2 1 0 3 4 样例 #1 解释: 对于第一组数据,可以用 2 次操作把 S 改为 abcabc,每相邻三个字符组成的字符串分别是 abc、bca、cab、abc,都满足包含的字符互不相同。 对于第三组数据,请注意可以不进行操作。 数据范围: 对于所有数据,保证 1 &le; T &le; 10⁵,1 &le; &sum;N &le; 10⁶,1 &le; K &le; 13,S 中只含有小写字符。 子任务说明: 子任务 1:&sum;N &le; 10,K &le; 10,分值 10 子任务 2&sum;N &le; 100,K &le; 10,分值 10 子任务 3:&sum;N &le; 10&sup3;,K &le; 13,分值 10 子任务 4:&sum;N &le; 10⁵,K &le; 3,分值 20 子任务 5:&sum;N &le; 10⁵,K &le; 13,分值 20 子任务 6:&sum;N &le; 10⁶,K &le; 13,分值 30 用C++ 14 With O2的语言写一段代码,不要超时,不要注释,用万能头,变量名用一个字母表示,最终代码写好后随便写5组边缘样例测试一下刚刚写的代码(把代码放进编译器输出检查),然后根据刚刚的代码测试修改代码,将这两段代码里的变量名用一个字母替代。代码加上空格,使代码有可读性(实在拿不到全分,也可以拿部分分,尽量多拿!)注意加上文件读写!!!!!!!!!!
最新发布
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值