> 文章列表 > AtCoder Beginner Contest 295——A-D讲解

AtCoder Beginner Contest 295——A-D讲解

AtCoder Beginner Contest 295——A-D讲解

蒟蒻来讲题,还望大家喜。若哪有问题,大家尽可提!

Hello, 大家好哇!本初中生蒟蒻讲解一下AtCoder Beginner Contest 295这场比赛的A-D题

===========================================================================================

A - Probably English

Problem Statement

You are given NNN strings W1,W2,…,WNW_1,W_2,\\dots,W_NW1,W2,,WN consisting of lowercase English letters.

If one or more of these strings equal and, not, that, the, or you, then print Yes; otherwise, print No.

Constraints

NNN is an integer between 111 and 100100100, inclusive.
1≤∣Wi∣≤501 \\le |W_i| \\le 501Wi50 (∣Wi∣|W_i|Wi is the length of WiW_iWi.)
WiW_iWi consists of lowercase English letters.

Input

The input is given from Standard Input in the following format:
NNN
W1W_1W1 W2W_2W2 …\\dots WNW_NWN

Output

Print the answer.

Sample Input 1

10
in that case you should print yes and not no

Sample Output 1

Yes
We have, for instance, W4=W_4=W4= you, so you should print Yes.

Sample Input 2

10
in diesem fall sollten sie no und nicht yes ausgeben

Sample Output 2

No
None of the strings WiW_iWi equals any of and, not, that, the, and you.


思路

呜呜呜,太简单


代码

#include <iostream>
#include <cstring>
#include <algorithm>
#define endl '\\n'
#define psb(i) push_back(i)
#define ppb() pop_back()
#define psf(i) push_front(i)
#define ppf() pop_front()
#define ps(i) push(i)using namespace std;typedef pair<int, int> PII;string cmp[6] = {"and", "not", "that", "the", "you"};inline int read()
{int w = 1, s = 0;char c = getchar();while (c < '0' || c > '9'){if (c == '-') w = -1;c = getchar();}while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();return w * s;
}int main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int n;string c;cin >> n;while (n --){cin >> c;for (int i = 0; i < 5; i ++)if (c == cmp[i]){cout << "Yes" << endl;return 0;}}cout << "No" << endl;return 0;
}

B - Bombs

原题

Problem Statement

We have a board with RRR rows running horizontally and CCC columns running vertically. Let (i,j)(i,j)(i,j) denote the square at the iii-th row from the top and jjj-th column from the left.
You are given characters Bi,jB_{i,j}Bi,j representing the current states of (i,j)(i,j)(i,j).
. represents an empty square; # represents a square with a wall; 1, 2,…\\dots, 9 represent a square with a bomb of power 1,2,…,91,2,\\dots,91,2,,9, respectively.
At the next moment, all bombs will explode simultaneously.
When a bomb explodes, every square whose Manhattan distance from the square with the bomb is not greater than the power of the bomb will turn into an empty square.
Here, the Manhattan distance from (r1,c1)(r_1,c_1)(r1,c1) to (r2,c2)(r_2,c_2)(r2,c2) is ∣r1−r2∣+∣c1−c2∣|r_1-r_2|+|c_1-c_2|r1r2+c1c2.
Print the board after the explosions.

Constraints

1≤R,C≤201\\leq R,C \\leq 201R,C20
RRR and CCC are integers.
Each Bi,jB_{i,j}Bi,j is one of ., #, 1, 2, …\\dots, 9.

Input

The input is given from Standard Input in the following format:
RRR CCC
B1,1B1,2…B1,CB_{1,1}B_{1,2}\\dots B_{1,C}B1,1B1,2B1,C
⋮\\vdots
BR,1BR,2…BR,CB_{R,1}B_{R,2}\\dots B_{R,C}BR,1BR,2BR,C

Output

Print RRR lines representing the board after the explosions. Use the format used in the input (do not print RRR or CCC).

Sample Input 1

4 4
.1.#
###.
.#2.
#.##

Sample Output 1

…#
#…

#…
AtCoder Beginner Contest 295——A-D讲解
The explosion of the bomb at (1,2)(1,2)(1,2) will turn the blue squares and purple squares in the above figure into empty squares.
The explosion of the bomb at (3,3)(3,3)(3,3) will turn the red squares and purple squares in the above figure into empty squares.
As seen in this sample, the explosion ranges of bombs may overlap.

Sample Input 2

2 5
…#.#
###.#

Sample Output 2

…#.#
###.#
There may be no bombs.

Sample Input 3

2 3
11#

Sample Output 3


…#

Sample Input 4

4 6
#.#3#.
###.#.
##.###
#1…#.

Sample Output 4


#…
#…#
…#.


思路

我们直接四重循环,先循环每个炸弹,再循环能炸到的点。


代码

#include <iostream>
#include <cstring>
#include <algorithm>
#define endl '\\n'
#define psb(i) push_back(i)
#define ppb() pop_back()
#define psf(i) push_front(i)
#define ppf() pop_front()
#define ps(i) push(i)using namespace std;typedef pair<int, int> PII;const int N = 2e1 + 10;int n, m;
char a[N][N];inline int read()
{int w = 1, s = 0;char c = getchar();while (c < '0' || c > '9'){if (c == '-') w = -1;c = getchar();}while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();return w * s;
}int main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> n >> m;for (int i = 1; i <= n; i ++)for (int j = 1; j <= m; j ++)cin >> a[i][j];for (int x1 = 1; x1 <= n; x1 ++)for (int y1 = 1; y1 <= m; y1 ++){if (!isdigit(a[x1][y1])) continue;int t = a[x1][y1] - '0';for (int x2 = 1; x2 <= n; x2 ++)for (int y2 = 1; y2 <= m; y2 ++)if ((!isdigit(a[x2][y2]) || (x1 == x2 && y1 == y2)) && abs(x1 - x2) + abs(y1 - y2) <= t)a[x2][y2] = '.';//, printf("(%d, %d)->(%d, %d)\\n", x1, y1, x2, y2);}for (int i = 1; i <= n; i ++){for (int j = 1; j <= m; j ++)cout << a[i][j];cout << endl;}return 0;
}

C - Socks

原题

Problem Statement

You have NNN socks. The color of the iii-th sock is AiA_iAi.
You want to perform the following operation as many times as possible. How many times can it be performed at most?
Choose two same-colored socks that are not paired yet, and pair them.

Constraints

1≤N≤5×1051\\leq N \\leq 5\\times 10^51N5×105
1≤Ai≤1091\\leq A_i \\leq 10^91Ai109
All values in the input are integers.

Input

The input is given from Standard Input in the following format:
NNN
A1A_1A1 A2A_2A2 …\\dots ANA_NAN

Output

Print an integer representing the answer.

Sample Input 1

6
4 1 7 4 1 4

Sample Output 1

2
You can do the operation twice as follows.
Choose two socks with the color 111 and pair them.
Choose two socks with the color 444 and pair them.
Then, you will be left with one sock with the color 444 and another with the color 777, so you can no longer do the operation.
There is no way to do the operation three or more times, so you should print 222.

Sample Input 2

1
158260522

Sample Output 2

0

Sample Input 3

10
295 2 29 295 29 2 29 295 2 29

Sample Output 3

4


思路

直接看看有多少个重复的颜色就行~~~


代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#define endl '\\n'
#define psb(i) push_back(i)
#define ppb() pop_back()
#define psf(i) push_front(i)
#define ppf() pop_front()
#define ps(i) push(i)using namespace std;typedef pair<int, int> PII;const int N = 5e5 + 10;int n;
int a[N];
unordered_map<int, int> cnt;inline int read()
{int w = 1, s = 0;char c = getchar();while (c < '0' || c > '9'){if (c == '-') w = -1;c = getchar();}while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();return w * s;
}int main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> n;for (int i = 1; i <= n; i ++)cin >> a[i], cnt[a[i]] ++;int res = 0;for (auto c : cnt)res += (c.second / 2);cout << res << endl;return 0;
}

D - Three Days Ago

原题

Problem Statement

The string 20230322 can be rearranged into 02320232, which is a repetition of 0232 twice.

Similarly, a string consisting of digits is said to be happy when it can be rearranged into (or already is) a repetition of some string twice.

You are given a string SSS consisting of digits. Find the number of pairs of integers (l,r)(l,r)(l,r) satisfying all of the following conditions.
1≤l≤r≤∣S∣1 \\le l \\le r \\le |S|1lrS. (∣S∣|S|S is the length of SSS.)
The (contiguous) substring formed of the lll-th through rrr-th characters of SSS is happy.

Constraints

SSS is a string consisting of digits whose length is between 111 and 5×1055 \\times 10^55×105, inclusive.

Input

The input is given from Standard Input in the following format:
SSS

Output

Print an integer representing the answer.

Sample Input 1

20230322

Sample Output 1

4
We have S=S=S= 20230322.
Here are the four pairs of integers (l,r)(l,r)(l,r) that satisfy the condition: (1,6)(1,6)(1,6), (1,8)(1,8)(1,8), (2,7)(2,7)(2,7), and (7,8)(7,8)(7,8).

Sample Input 2

0112223333444445555556666666777777778888888889999999999

Sample Output 2

185
SSS may begin with 0.

Sample Input 3

3141592653589793238462643383279502884197169399375105820974944

Sample Output 3

9


思路

本题主要意思是有多少个区间满足区间内的值随便排列能够组成两个相同的序列。例如:202303,就可以排列成230230,这就是一个满足条件的序列!通过观察,要想凑出两个相同的小序列,必须序列中的每一个数出现的次数都是偶数,这样才能平均的分配到两个小序列中。
知道这一点之后,明确一下本题的目标:找到有多少个区间其中的数出现的次数都是偶数,这里我们可以用到异或(这一篇写的很好,值得一看)。因为,每一个数只能取1-9,所以我们用一个01序列来表示他们的奇偶(0表示偶数,1表示奇数)。

例如:S=20230322

R[i]表示前i个数中每个数出现的次数的奇偶
i = 0  0000000000
i = 1  0010000000
i = 2  1010000000
i = 3  1000000000
i = 4  1001000000
i = 5  0001000000
i = 6  0000000000
i = 7  0010000000
i = 8  0000000000

Ri=Rj(i<j)R_i = R_j(i < j)Ri=Rj(i<j),则从i+1i+1i+1jjj一定是一个满足条件的序列。

证明:从i+1i + 1i+1jjj的奇偶性序列通过前缀和的思想,其实就是Rj−Ri(Rj≥Ri)R_j - R_i(R_j \\geq R_i)RjRi(RjRi)(注意不是i+1, 因为要取i+1),若Rj−RiR_j - R_iRjRi中所有的数全部是偶数,那么序列中的体现就是0000000000,也就是Rj−Ri=0R_j - R_i = 0RjRi=0,即:Ri=RjR_i = R_jRi=Rj,则从i+1i+1i+1jjj一定是一个满足条件的序列。

所以找到所有的Ri=RjR_i = R_jRi=Rj的个数,即为答案!


代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#define endl '\\n'
#define int long long
#define psb(i) push_back(i)
#define ppb() pop_back()
#define psf(i) push_front(i)
#define ppf() pop_front()
#define ps(i) push(i)using namespace std;typedef pair<int, int> PII;string s;
unordered_map<int, int> times;
int state;
int res = 0;inline int read()
{int w = 1, s = 0;char c = getchar();while (c < '0' || c > '9'){if (c == '-') w = -1;c = getchar();}while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();return w * s;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> s;times[0] = 1; //全是0就是一种可行的方案!for (int i = 0; i < (int)s.size(); i ++){state ^= (1 << (s[i] - '0')); //我们可以通过二进制来存储奇偶性序列res += times[state]; //每一次加上之前的所有可以的,因为对于每一个小于j的i,都要记录times[state] ++; //出现了一次,不断累加}cout << res << endl;return 0;
}

今天就到这里了!

大家有什么问题尽管提,我都会尽力回答的!

吾欲您伸手,点的小赞赞。吾欲您喜欢,点得小关注!

(这两天比赛有点多,直到现在才发出来,非常抱歉~~~)