本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@TOC
2021牛客暑期多校训练营1 - D - Determine the Photo Position
传送门 Time Limit: 1 second Memory Limit: 262144K
题目描述
You have taken the graduation picture of graduates. The picture could be regarded as a matrix of , each element in A is 0 or 1, representing a blank background or a student, respectively.
However, teachers are too busy to take photos with students and only took a group photo themselves. The photo could be regarded as a matrix of where each element is 2 representing a teacher.
As a master of photoshop, your job is to put photo B into photo A with the following constraints: you are not allowed to split, rotate or scale the picture, but only translation. each element in matrix B should overlap with an element in A completely, and each teacher should overlap with a blank background, not shelter from a student.
Please calculate the possible ways that you can put photo B into photo A.
输入描述:
The first line contains two integers indicating the size of photos A and B.
In the next lines,each line contains characters of '0' or '1',representing the matrix .
The last line contains characters of '2', representing matrix .
输出描述:
Output one integer in a line, indicating the answer.
样例
样例一
输入
5 3
00000
01110
01110
01110
00000
222
输出
6
样例二
输入
3 2
101
010
101
22
输出
0
样例三
输入
3 1
101
010
101
2
输出
4
题目大意
拍毕业照有个位置,其中0是空位1是学生 有个老师(一排挨着) 问有多少种方式能把老师正好插入到学生的空位中(老师仍然一排挨着)
解题思路
那就对于学生的每一排,看这一排中有多少个连续的空位。 连续空位大于等于个就有种插入方式。 连续空位数小于则不能插入。
比如长度为3的漂亮数组的最大值是,那么8可表示成是长度为3的漂亮数组。
AC代码
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int getOne(int lx, int m) // 参数:连续的空位数、要插入的老师数 | 返回:有多少种插入方式
{
if(lx<m)return 0; // 空位数小于老师数,不能插入,插入方式是0
return lx-m+1; // 空位数≥老师数,插入方式是空位数-老师数+1
}
int main()
{
int n,m;
cin>>n>>m;
int ans=0;
for(int i=0;i<n;i++)
{
string s;
cin>>s;
s = '1'+s+'1'; // 前后各添加一个学生,对空位不产生影响,相当于是哨兵,最后必定会遍历到1,遍历到1就把连续的空位累加到结果中。
int lx=0; // 连续的空位数
for(int j=1;j<=n+1;j++) // 遍历这一排
{
if(s[j]=='0')lx++; // 是空位,连续的空位数就+1
else // 不是空位,前面的空位(如果有的话)将不连续
{
ans+=getOne(lx,m); // 累加到结果中
lx=0; // 连续的空位数变成0
}
}
}
cout<<ans<<endl; // 输出结果即可。
return 0;
}
同步发文于我的CSDN,原创不易,转载请附上原文链接哦~
Tisfy:letmefly.blog.csdn.net/article/det…