2021.1.10寒假打卡Day7

哈尔滨学院2021ACM冬令营训练赛(一)

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

Note
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”.

#include<iostream>
#include<string.h>
using namespace std;

int main(){
	int t,len;
	char a[1005];
	cin>>t;
	while(t--){
		cin>>a;
		len=strlen(a);
		for(int j=0;j<len-1;j+=2) cout<<a[j];
		cout<<a[len-1]<<endl;
	} 
	return 0;
}

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 i mod 2 = a[i] mod 2 holds, where x mod 2 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: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 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.

#include<iostream>
using namespace std;

int main(){
	int t,n,a[40],ji=0,ou=0;
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=0;i<n;i++){
			cin>>a[i];
			if(i%2 != a[i]%2){
				if(i%2==0) ++ou;
				else ++ji;
			}
		}
		if(ji!=ou){
			cout<<-1<<endl;
			continue;
		}else{
			cout<<ji<<endl;
		}
		ji=ou=0;
	}
	return 0;
}

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.

既然要求间隔k坐座位,那把1个人当作2*k+1个人即可,把每个人左右两边k个位置都填入1,剩下的空位再判断能做几个,若有连续空位,那就一个一个入座模拟一下,看看最多坐几个。

#include<iostream>
using namespace std;
char c[200000];

int main(){
	int t,n,r,k,ans=0;
	cin>>t;
	while(t--){
		cin>>n>>k>>c;
		r=n;
		for(int i=0;i<r;i++){
			if((int)c[i]){
				if(i>k&&r-i-1>k){
					for(int j=i-k;j<=i+k;j++)
						c[j]='1';
				}
				else if(i<k&&r-i-1<k){
					break;
				}
				else if(i<k){
					for(int j=0;j<=i+k;j++)
						c[j]='1';
				}
				else if(r-i-1<k){
					for(int j=i-k;j<=r-1;j++)
						c[j]='1';
				}
			}
			/*for(int i=0;i<r;i++){
				if(!(int)c[i]) ++ans;
			}*/				//统计空位还没完成
		}
		cout<<ans<<endl;
	}
	return 0;
}

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

Note
In the first test case, such strings t are suitable: "aac’, “aab”.

In the second test case, such trings t are suitable: “a”, “b”, “c”.

In the third test case, only the string t equals to “aba” is suitable, but the character ‘b’ can be from the second or third position.

E. Required Remainder

You are given three integers x,y and n. Your task is to find the maximum integer k such that 0≤k≤n that k mod x=y, where mod is modulo operation. Many programming languages use percent operator % to implement it.

In other words, with given x,y and n you need to find the maximum possible integer from 0 to n that has the remainder y modulo x.

You have to answer t independent test cases. It is guaranteed that such k exists for each test case.

Input
The first line of the input contains one integer t (1≤t≤5⋅104) — the number of test cases.

The next t lines contain test cases.

The only line of the test case contains three integers x,y and n (2≤x≤109; 0≤y<x; y≤n≤109).

It can be shown that such k always exists under the given constraints.

Output
For each test case, print the answer — maximum non-negative integer k such that 0≤k≤n and kmodx=y. It is guaranteed that the answer always exists.

Example
input

7
7 5 12345
5 0 4
10 5 15
17 8 54321
499999993 9 1000000000
10 5 187
2 0 999999999

output

12339
0
15
54306
999999995
185
999999998

Note
In the first test case of the example, the answer is 12339=7⋅1762+5 (thus, 12339mod7=5). It is obvious that there is no greater integer not exceeding 12345 which has the remainder 5 modulo 7.

#include<iostream>
using namespace std;

int main(){
	int t,n,x,y,i;
	cin>>t;
	while(t--){
		cin>>x>>y>>n;
		for(i=0;x*i+y<=n;i++);
		cout<<x*(i-1)+y<<endl;
	}
	return 0;
}
#include<iostream>
using namespace std;

int main(){
	int t,n,x,y,i;
	cin>>t;
	while(t--){
		cin>>x>>y>>n;
		while((n-y)%x!=0){
			n-=1;
		}
		cout<<n<<endl;
	}
	return 0;
}

很遗憾,上面这两个都TLE了 QAQ
条件 >> 思考:
ans%x=y >> (ans-y)%x==0
ans ≤ n >> ans=n-(n-y)%x

#include<iostream>
using namespace std;

int main(){
	int t,n,x,y;
	cin>>t;
	while(t--){
		cin>>x>>y>>n;
		cout<<n-(n-y)%x<<endl;
	}
	return 0;
}

F. Multiply by 2, divide by 6

You are given an integer n. In one move, you can either multiply n by two or divide n by 6 (if it is divisible by 6 without the remainder).

Your task is to find the minimum number of moves needed to obtain 1 from n or determine if it’s impossible to do that.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow.

The only line of the test case contains one integer n (1≤n≤109).

Output
For each test case, print the answer — the minimum number of moves needed to obtain 1 from n if it’s possible to do that or -1 if it’s impossible to obtain 1 from n.

Example
Input

7
1
2
3
12
12345
15116544
387420489

Output

0
-1
2
-1
-1
12
36

Note
Consider the sixth test case of the example. The answer can be obtained by the following sequence of moves from the given integer 15116544:

Divide by 6 and get 2519424;
divide by 6 and get 419904;
divide by 6 and get 69984;
divide by 6 and get 11664;
multiply by 2 and get 23328;
divide by 6 and get 3888;
divide by 6 and get 648;
divide by 6 and get 108;
multiply by 2 and get 216;
divide by 6 and get 36;
divide by 6 and get 6;
divide by 6 and get 1.

吃不消了,明天肝>︿<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值