Codeforces Round #650 (Div. 3) ABC

在这里插入图片描述
A. Short Substrings
A题入口:https://codeforces.com/contest/1367/problem/A
题目描述

Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters.
The string a has a length of 2 or more characters.
Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a=“abac”, then all the substrings of length 2 of the string a are: “ab”, “ba”, “ac”.
Therefore, the string b=“abbaac”.
You are given the string b.
Help Alice to guess the string a that Bob came up with.
It is guaranteed that b was built according to the algorithm given above.
It can be proved that the answer to the problem is unique.

Input

The first line contains a single positive integer t (1≤t≤1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2≤|b|≤100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.

Output

Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.

Example

Example
inputCopy
4
abbaac
ac
bccddaaf
zzzzzzzzzz
outputCopy
abac
ac
bcdaf
zzzzzz

Notes

The first test case is explained in the statement.
In the second test case, Bob came up with the string a=“ac”, the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a=“bcdaf”, substrings of length 2 of string a are: “bc”, “cd”, “da”, “af”, so the string b=“bccddaaf”.

A题题解:Short Substrings
题意: 给出字符串b按要求得出字符串a
思路:(做题的时候我是用纸算的)直接看例子
例子:
字符串b:

 a        b        b       a       c        c
b[1]     b[2]     b[3]    b[4]    b[5]     b[6]

字符串a:

 a      b      a     c
a[1]  a[2]   a[3]  a[4]

已知字符串b即已知字符串b的长度
通过观察得出规律:
两字符串之间的长度规律:b.length() = 2×(a.length() - 1)
两字符串之间的数值规律:a[i]=b[2i-1]
上AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
	int t;
	scanf_s("%d", &t);
	getchar();
	while (t--)
	{
		char b[105];
		scanf_s("%s", b);
		for (int i = 0; i < strlen(b) - 1; i += 2)
		{
			printf("%c", b[i]);
		}
		printf("%c\n", b[strlen(b) - 1]);
	}
	return 0;
}

B. Even Array
B题入口:https://codeforces.com/contest/1367/problem/B

题目描述

You are given an array a[0…n−1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0≤i≤n−1) the equality imod2=a[i]mod2 holds, where xmod2 is the remainder of dividing x by 2.
For example, the arrays [0,5,2,1] and [0,17,0,3] are good, and the array [2,4,6,7] is bad, because for i=1, the parities of i and a[i] are different: imod2=1mod2=1, but a[i]mod2=4mod2=0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.

Input

The first line contains a single integer t (1≤t≤1000) — the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1≤n≤40) — the length of the array a.
The next line contains n integers a0,a1,…,an−1 (0≤ai≤1000) — the initial array.

Output

The first line contains a single integer t (1≤t≤1000) — the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1≤n≤40) — the length of the array a.
The next line contains n integers a0,a1,…,an−1 (0≤ai≤1000) — the initial array.

Example

input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
output
2
1
-1
0

Note

In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good.

B题题解:

B Even Array
题意: 给一个有n个元素的数组,下标从0开始,通过交换数组中的两个数使得每个元素与该元素下标的奇偶性相同,求最小交换次数,如果无论如何怎么交换也无法达到目的则输出-1。
*思路:

  1. 奇数与奇数交换和偶数与偶数交换没有意义。
  2. 通过对a[i]%2!=i%2 的判断分别计算出: (1) 不符合要求数字总数kase (2) kase中偶数的数量 (3) kase中奇数的数量
  3. 要满足条件必须满足 kase中的奇数==偶数

上代码:

#include<iostream>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, a[100], kase = 0, b1 = 0, b2 = 0;
        cin >> n;
        for (int i = 0; i < n; i++) //输入数组
            cin >> a[i];
        for (int i = 0; i < n; i++)
        {
            if (a[i] % 2 != i % 2) //a[i]%2 ≠ i%2(此时a[i]与i的奇偶性不同)
            {
                kase++;//计算次数
                if (a[i] % 2 == 0)
                    b1++;//a[i]是偶数的数量
                else
                    b2++;//a[i]是奇数的数量
            }
        }
        if (kase % 2 != 0)//如果kase的数量不能整除2,则无论如何也不可能交换成功
            cout << -1 << endl;
        else
        {
            if (b1 == b2)//a[i]奇数偶数相同的时候,经过kase/2次就可以达成目的
                cout << kase / 2 << endl;
            else 
                cout << -1 << endl;
        }
    }
return 0;
}

C. Social Distance
C题入口:https://codeforces.com/contest/1367/problem/C
题目描述

Polycarp and his friends want to visit a new restaurant. The restaurant has n tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from 1 to n in the order from left to right. The state of the restaurant is described by a string of length n which contains characters “1” (the table is occupied) and “0” (the table is empty).
Restaurant rules prohibit people to sit at a distance of k or less from each other. That is, if a person sits at the table number i, then all tables with numbers from i−k to i+k (except for the i-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than k.
For example, if n=8 and k=2, then:
strings “10010001”, “10000010”, “00000000”, “00100000” satisfy the rules of the restaurant;
strings “10100100”, “10011001”, “11111111” do not satisfy to the rules of the restaurant, since each of them has a pair of “1” with a distance less than or equal to k=2.
In particular, if the state of the restaurant is described by a string without “1” or a string with one “1”, then the requirement of the restaurant is satisfied.
You are given a binary string s that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string s.
Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of “0” that can be replaced by “1” such that the requirement will still be satisfied?
For example, if n=6, k=1, s= “100010”, then the answer to the problem will be 1, since only the table at position 3 can be occupied such that the rules are still satisfied.

Input

The first line contains a single integer t (1≤t≤104) — the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing two integers n and k (1≤k≤n≤2⋅105) — the number of tables in the restaurant and the minimum allowed distance between two people.
The second line of each test case contains a binary string s of length n consisting of “0” and “1” — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two “1” is more than k.
The sum of n for all test cases in one test does not exceed 2⋅105.

Output

For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output 0.

Example

input
6
6 1
100010
6 2
000000
5 1
10101
3 1
001
2 2
00
1 1
0
output
1
2
0
1
1
1

Note

The first test case is explained in the statement.
In the second test case, the answer is 2, since you can choose the first and the sixth table.
In the third test case, you cannot take any free table without violating the rules of the restaurant.

C题题解

C Social Distance
题意:给定一个长度为n的字符串(只含0和1),且要求每个1相差k个位置,要求计算出,该字符串中有几个0在符合上述条件下可以换成1。
思路:暴力解法显然不能通(虽然也提交了一次暴力解法) 从左边往右边枚举,如果是0,则判断是否可以将其变为1,如果可以,计数,如果不可以,则直接跳过k个数字。继续枚举。

上代码

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        char A[200005];
        int n, k, total = 0;
        cin >> n >> k >> A;
        for (int i = 0; i < n; )
        {
            if (A[i] == '1')
            {
                i += (k + 1);//如果是1,直接跳过k个数字
            }
            else if (A[i] == '0')
            {
                int flag = 1;
                for (int j = i - k; j <= i + k; j++)//从i-k枚举到i+k
                {
                    if (j < 0)//index不能小于0
                        continue;
                    else if (j >= n)
                        break;
                    else if (A[j] == '1')
                    {
                        i = j;
                        flag = 0;
                        break;
                    }
                }
                if (flag == 0)
                    i += (k + 1);//跳
                else if (flag == 1)
                {
                    A[i] = '1';
                    total++;
                    i += (k + 1);//跳
                }
            }
        }
        cout << total << endl;
    }
    return 0;
}

大一入门算法小白记录一下自己的成长历程,路过的大佬们勿怪!!!

猜你喜欢

转载自blog.csdn.net/m0_46272108/article/details/106848475
今日推荐