Codeforces Round #650 (Div. 3) (A-D)

[传送门]

A. Short Substrings

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

input

4
abbaac
ac
bccddaaf
zzzzzzzzzz

output

abac
ac
bcdaf
zzzzzz

my Accept code

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define ll long long
#define db double
#define inf INT_MAX
#define s(a, n) memset(a, n, sizeof(a))
#define g(a, max) fgets(a, max, stdin)
#define debug(a) cout << '#' << a << '#' << endl

using namespace std;
bool fi = true;
const unsigned long long MOD = 1e9 + 7;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        string a;
        cin >> a;
        string ans = "";
        for (int j = 0; j < a.size(); j += 2) {
            ans = ans + a[j];
        }
        ans += a[a.size() - 1];
        cout << ans << endl;
    }
}

在这里插入图片描述

B. Even Array

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

For each test case, output a single integer — the minimum number of moves to make the given array a good, or -1 if this is not possible.

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.

my Accept code

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define ll long long
#define db double
#define inf INT_MAX
#define s(a, n) memset(a, n, sizeof(a))
#define g(a, max) fgets(a, max, stdin)
#define debug(a) cout << '#' << a << '#' << endl

using namespace std;
bool fi = true;
const unsigned long long MOD = 1e9 + 7;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    int a[50];
    bool g[50];
    int os, js;
    for (int i = 0; i < t; i++) {
        int n;
        cin >> n;
        os = js = 0;
        s(g, false);
        bool allg = true;
        int swap = 0;
        for (int j = 0; j < n; j++) {
            cin >> a[j];
            // cout<<(a[j]&1)<
            // cout<<(j&1)<
            if ((a[j] & 1) == (j & 1)) {
                g[j] = true;
            } else {
                // debug(j);
                swap++;
                allg = false;
            }
            if (a[j] & 1)
                js++;
            else
                os++;
        }
        // debug(allg);
        if (allg)
            cout << 0 << endl;
        else {
            if ((n & 1) == 0) {
                if (os != js)
                    cout << -1 << endl;
                else {
                    cout << swap / 2 << endl;
                }
            } else {
                if (os - js == 1) {
                    cout << swap / 2 << endl;
                } else {
                    cout << -1 << endl;
                }
            }
        }
    }
}

在这里插入图片描述

C. Social Distance

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.

my Accept code

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define ll long long
#define db double
#define inf INT_MAX
#define s(a, n) memset(a, n, sizeof(a))
#define g(a, max) fgets(a, max, stdin)
#define debug(a) cout << '#' << a << '#' << endl

using namespace std;
bool fi = true;
const unsigned long long MOD = 1e9 + 7;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        string a;
        cin >> a;
        int flag = 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] == '1') {
                flag = 2 * k;
            } else {
                if (flag == 0) {
                    ans++;
                    // debug(i);
                    flag = k;
                } else {
                    flag--;
                }
            }
        }
        if (flag < k) ans++;
        cout << ans << endl;
    }
}

在这里插入图片描述

D. Task On The Board

Polycarp wrote on the board a string s containing only lowercase Latin letters (‘a’-‘z’). This string is known for you and given in the input.

After that, he erased some letters from the string s, and he rewrote the remaining letters in any order. As a result, he got some new string t. You have to find it with some additional information.

Suppose that the string t has length m and the characters are numbered from left to right from 1 to m. You are given a sequence of m integers: b1,b2,…,bm, where bi is the sum of the distances |i−j| from the index i to all such indices j that tj>ti (consider that ‘a’<‘b’<…<‘z’). In other words, to calculate bi, Polycarp finds all such indices j that the index j contains a letter that is later in the alphabet than ti and sums all the values |i−j|.

For example, if t = “abzb”, then:

  • since t1=‘a’, all other indices contain letters which are later in the alphabet, that is: b1=|1−2|+|1−3|+|1−4|=1+2+3=6;
  • since t2=‘b’, only the index j=3 contains the letter, which is later in the alphabet, that is: b2=|2−3|=1;
  • since t3=‘z’, then there are no indexes j such that tj>ti, thus b3=0;
  • since t4=‘b’, only the index j=3 contains the letter, which is later in the alphabet, that is: b4=|4−3|=1.

Thus, if t = “abzb”, then b=[6,1,0,1].

Given the string s and the array b, find any possible string t for which the following two requirements are fulfilled simultaneously:

  • t is obtained from s by erasing some letters (possibly zero) and then writing the rest in any order;
  • the array, constructed from the string t according to the rules above, equals to the array b specified in the input data.

Input

The first line contains an integer q (1≤q≤100) — the number of test cases in the test. Then q test cases follow.

Each test case consists of three lines:

the first line contains string s, which has a length from 1 to 50 and consists of lowercase English letters;
the second line contains positive integer m (1≤m≤|s|), where |s| is the length of the string s, and m is the length of the array b;
the third line contains the integers b1,b2,…,bm (0≤bi≤1225).
It is guaranteed that in each test case an answer exists.

Output

Output q lines: the k-th of them should contain the answer (string t) to the k-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any.

Example

input

4
abac
3
2 1 0
abc
1
0
abba
3
1 0 1
ecoosdcefr
10
38 13 24 14 11 5 3 24 17 0

output

aac
b
aba
codeforces

my Accept code

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define ll long long
#define db double
#define inf INT_MAX
#define s(a, n) memset(a, n, sizeof(a))
#define g(a, max) fgets(a, max, stdin)
#define debug(a) cout << '#' << a << '#' << endl

using namespace std;
bool fi = true;
const unsigned long long MOD = 1e9 + 7;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    int num[30];
    while (t--) {
        string s;
        cin >> s;
        int slen = s.size();
        s(num, 0);
        for (int i = 0; i < slen; i++) num[s[i] - 'a']++;
        int dist[55];
        int n;
        cin >> n;
        char ans[55];
        ans[n] = '\0';
        for (int i = 0; i < n; i++) cin >> dist[i];
        int sit = 0, l = 25;
        while (sit < n) {
            vector<int> zero;
            for (int i = 0; i < n; i++)
                if (dist[i] == 0) zero.push_back(i);
            int zs = zero.size();
            while (num[l] < zs) l--;
            for (int i = 0; i < n; i++) {
                if (dist[i] == 0)
                    dist[i] = -1, ans[i] = 'a' + l, sit++;
                else if (dist[i] == -1)
                    continue;
                else
                    for (int j = 0; j < zs; j++) dist[i] -= abs(i - zero[j]);
            }
            l--;
        }
        cout << ans << endl;
    }
}

在这里插入图片描述

你可能感兴趣的:(cf题解)