第二周————STL库的学习

本周也是开始学一些过去没有接触的新东西,从标题看得出来就是STL库,这是一个包含非常多有用的函数库,对我的学习而言,可以从之前只知道数组的我来讲,算是个很大很大的提升。不过呢,这学起来可没那么轻松,从我做的题目就看得出来这并没有那么简单。

(一)例题

A-Spell Check

Timur likes his name. As a spelling of his name, he allows any permutation of the letters of the name. For example, the following strings are valid spellings of his name: Timur, miurT, Trumi, mriTu. Note that the correct spelling must have uppercased T and lowercased other letters.

Today he wrote string s𝑠 of length n𝑛 consisting only of uppercase or lowercase Latin letters. He asks you to check if s𝑠 is the correct spelling of his name.

Input

The first line of the input contains an integer t𝑡 (1≤t≤1031≤𝑡≤103) — the number of test cases.

The first line of each test case contains an integer n𝑛 (1≤n≤10)(1≤𝑛≤10) — the length of string s𝑠.

The second line of each test case contains a string s𝑠 consisting of only uppercase or lowercase Latin characters.

Output

For each test case, output "YES" (without quotes) if s𝑠 satisfies the condition, and "NO" (without quotes) otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

Examples

InputcopyOutputcopy
10
5
Timur
5
miurT
5
Trumi
5
mriTu
5
timur
4
Timr
6
Timuur
10
codeforces
10
TimurTimur
5
TIMUR
YES
YES
YES
YES
NO
NO
NO
NO
NO
NO

1.这一题算简单的了,看着很复杂,其实就只需要找5个对应的字母即可。

2.每个字母对应设为0,找到就变1,前提是字符串的长度刚好为5哦,而且题目要求本来答案就是长度为5。最后再判断每个字符是否都出现过了,就ok了。

3.题解大致为

#include<bits/stdc++.h>
using namespace std;
int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int t,n,i,j;
	string x;
	cin>>t;
	for(i=0;i<t;i++){
		int a=0,b=0,c=0,d=0,e=0; 
		cin>>n>>x;
		if(n!=5){
			cout<<"NO"<<endl;
		}
		else{
			for(j=0;j<5;j++){
				if(x[j]=='T'){
					a=1;
				}
				else if(x[j]=='i'){
					b=1;
				}
				else if(x[j]=='m'){
					c=1;
				}
				else if(x[j]=='u'){
					d=1;
				}
				else if(x[j]=='r'){
					e=1;
				}
			}
			if(a&&b&&c&&d&&e){
				cout<<"YES"<<endl;
			}
			else{
				cout<<"NO"<<endl;
			}
		}
	}		
}

B-Make Majority

You are given a sequence [a1,…,an][𝑎1,…,𝑎𝑛] where each element ai𝑎𝑖 is either 00 or 11. You can apply several (possibly zero) operations to the sequence. In each operation, you select two integers 1≤l≤r≤|a|1≤𝑙≤𝑟≤|𝑎| (where |a||𝑎| is the current length of a𝑎) and replace [al,…,ar][𝑎𝑙,…,𝑎𝑟] with a single element x𝑥, where x𝑥 is the majority of [al,…,ar][𝑎𝑙,…,𝑎𝑟].

Here, the majority of a sequence consisting of 00 and 11 is defined as follows: suppose there are c0𝑐0 zeros and c1𝑐1 ones in the sequence, respectively.

  • If c0≥c1𝑐0≥𝑐1, the majority is 00.
  • If c0<c1𝑐0<𝑐1, the majority is 11.

For example, suppose a=[1,0,0,0,1,1]𝑎=[1,0,0,0,1,1]. If we select l=1,r=2𝑙=1,𝑟=2, the resulting sequence will be [0,0,0,1,1][0,0,0,1,1]. If we select l=4,r=6𝑙=4,𝑟=6, the resulting sequence will be [1,0,0,1][1,0,0,1].

Determine if you can make a=[1]𝑎=[1] with a finite number of operations.

Input

Each test contains multiple test cases. The first line contains the number of test cases t𝑡 (1≤t≤4⋅1041≤𝑡≤4⋅104). Description of the test cases follows.

The first line of each testcase contains one integer n𝑛 (1≤n≤2⋅1051≤𝑛≤2⋅105).

The second line of each testcase contains a string consisting of 00 and 11, describing the sequence a𝑎.

It's guaranteed that the sum of n𝑛 over all testcases does not exceed 2⋅1052⋅105.

Output

For each testcase, if it's possible to make a=[1]𝑎=[1], print YES. Otherwise, print NO. You can output the answer in any case (upper or lower). For example, the strings yEs, yes, Yes, and YES will be recognized as positive responses.

Examples

InputcopyOutputcopy
5
1
0
1
1
2
01
9
100000001
9
000011000
No
Yes
No
Yes
No

Note

In the fourth testcase of the example, initially a=[1,0,0,0,0,0,0,0,1]𝑎=[1,0,0,0,0,0,0,0,1]. A valid sequence of operations is:

  1. Select l=2,r=8𝑙=2,𝑟=8 and apply the operation. a𝑎 becomes [1,0,1][1,0,1].
  2. Select l=1,r=3𝑙=1,𝑟=3 and apply the operation. a𝑎 becomes [1][1].

1.这道题目我感觉出的很有意思,内容不多,而且思考起来也容易。

2.看着题目一大串的其实简单的理解就是,把多个连续的0缩成一个0,最后再比较0和1的个数就可以了,很有意思的一道题目。

3大致题解为

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	vector<char>a;
	int t,n,i,j;
	char x;
	cin>>t;
	while(t--){
		cin>>n;
		int t=0,m=0,f=0;
		for(i=0;i<n;i++){
			cin>>x;
			if(x=='0'&&f!=1){
				m++;f=1;
			}
			if(x=='1'){
				t++;f=0;
			}
		}
		if(t<=m)cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}	
}

4.没啥好多说的,这种题目多来点,感觉我还行!

C-Array Cloning Technique

You are given an array a𝑎 of n𝑛 integers. Initially there is only one copy of the given array.

You can do operations of two types:

  1. Choose any array and clone it. After that there is one more copy of the chosen array.
  2. Swap two elements from any two copies (maybe in the same copy) on any positions.

You need to find the minimal number of operations needed to obtain a copy where all elements are equal.

Input

The input consists of multiple test cases. The first line contains a single integer t𝑡 (1≤t≤1041≤𝑡≤104) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer n𝑛 (1≤n≤1051≤𝑛≤105) — the length of the array a𝑎.

The second line of each test case contains n𝑛 integers a1,a2,…,an𝑎1,𝑎2,…,𝑎𝑛 (−109≤ai≤109−109≤𝑎𝑖≤109) — the elements of the array a𝑎.

It is guaranteed that the sum of n𝑛 over all test cases does not exceed 105105.

Output

For each test case output a single integer — the minimal number of operations needed to create at least one copy where all elements are equal.

Examples

InputcopyOutputcopy
6
1
1789
6
0 1 3 3 7 0
2
-1000000000 1000000000
4
4 3 2 1
5
2 5 7 6 3
7
1 1 1 1 1 1 1
0
6
2
5
7
0

Note

In the first test case all elements in the array are already equal, that's why the answer is 00.

In the second test case it is possible to create a copy of the given array. After that there will be two identical arrays:

[ 0 1 3 3 7 0 ][ 0 1 3 3 7 0 ] and [ 0 1 3 3 7 0 ][ 0 1 3 3 7 0 ]

After that we can swap elements in a way so all zeroes are in one array:

[ 0 0– 0– 3 7 0 ][ 0 0_ 0_ 3 7 0 ] and [ 1– 1 3 3 7 3– ][ 1_ 1 3 3 7 3_ ]

Now let's create a copy of the first array:

[ 0 0 0 3 7 0 ][ 0 0 0 3 7 0 ], [ 0 0 0 3 7 0 ][ 0 0 0 3 7 0 ] and [ 1 1 3 3 7 3 ][ 1 1 3 3 7 3 ]

Let's swap elements in the first two copies:

[ 0 0 0 0– 0– 0 ][ 0 0 0 0_ 0_ 0 ], [ 3– 7– 0 3 7 0 ][ 3_ 7_ 0 3 7 0 ] and [ 1 1 3 3 7 3 ][ 1 1 3 3 7 3 ].

Finally, we made a copy where all elements are equal and made 66 operations.

It can be proven that no fewer operations are enough.

1.这道题目的思考难度不能想的太难,要化繁为简,看起来很多步骤,其实思考起来很容易的。

2.求操作次数,这有两个操作,可以复制,可以交换,最多交换2个其实没啥用,你爱交换几个就是几个,因为操作都最多算+1,然后,重要的就是计算操作的次数,反正能最少的复制出刚好的数量就行,比如0 0 3 3 7 1复制之后最大的数的个数就有4了,所以最多再复制一次就行了,剩下的再交换,这就是题目的思路了。

3.题解大致为

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	map<string,int>a;
	int t,n,i;
	string x;
	cin>>t;
	while(t--){
		cin>>n;
		for(i=0;i<n;i++){
			cin>>x;
			a[x]++;
		}
		int max=0;
		map<string,int>::iterator it=a.begin();
		for(it=a.begin();it!=a.end();it++){
			if(it->second>max){
				max=it->second;
			}
		}
		int cnt=0;
		while(max!=n){
			cnt++;
			if(n<=2*max){
				cnt+=n-max;
				max+=n-max;
			} 
			else{
				cnt+=max;
				max*=2;
			}
		}
		cout<<cnt<<endl;
		a.clear();
	}
}

E-Maximum Rounding

Given a natural number x𝑥. You can perform the following operation:

  • choose a positive integer k𝑘 and round x𝑥 to the k𝑘-th digit

Note that the positions are numbered from right to left, starting from zero. If the number has k𝑘 digits, it is considered that the digit at the k𝑘-th position is equal to 00.

The rounding is done as follows:

  • if the digit at the (k−1)(𝑘−1)-th position is greater than or equal to 55, then the digit at the k𝑘-th position is increased by 11, otherwise the digit at the k𝑘-th position remains unchanged (mathematical rounding is used).

  • if before the operations the digit at the k𝑘-th position was 99, and it should be increased by 11, then we search for the least position k′𝑘′ (k′>k𝑘′>𝑘), where the digit at the k′𝑘′-th position is less than 99 and add 11 to the digit at the k′𝑘′-th position. Then we assign k=k′𝑘=𝑘′.

  • after that, all digits which positions are less than k𝑘 are replaced with zeros.

Your task is to make x𝑥 as large as possible, if you can perform the operation as many times as you want.

For example, if x𝑥 is equal to 34513451, then if you choose consecutively:

  • k=1𝑘=1, then after the operation x𝑥 will become 34503450
  • k=2𝑘=2, then after the operation x𝑥 will become 35003500
  • k=3𝑘=3, then after the operation x𝑥 will become 40004000
  • k=4𝑘=4, then after the operation x𝑥 will become 00

To maximize the answer, you need to choose k=2𝑘=2 first, and then k=3𝑘=3, then the number will become 40004000.

Input

The first line contains a single integer t𝑡 (1≤t≤1041≤𝑡≤104) — the number of test cases.

Each test case consists of positive integer x𝑥 with a length of up to 2⋅1052⋅105. It is guaranteed that there are no leading zeros in the integer.

It is guaranteed that the sum of the lengths of all integers x𝑥 over all test cases does not exceed 2⋅1052⋅105.

Output

For each set of input data, output the maximum possible value of x𝑥 after the operations. The number should not have leading zeros in its representation.

Examples

InputcopyOutputcopy
10
1
5
99
913
1980
20444
20445
60947
419860
40862016542130810467
1
10
100
1000
2000
20444
21000
100000
420000
41000000000000000000

Note

In the first sample, it is better not to perform any operations.

In the second sample, you can perform one operation and obtain 1010.

In the third sample, you can choose k=1𝑘=1 or k=2𝑘=2. In both cases the answer will be 100100.

1.这一题算是看起来简单,做起来难的类型了,可以好好利用字符串而不是整数。

2.考虑两种情况,第一种是在第一位,第二种是第一位之后的了,然后从后面开始,每个大于5的,前面都+1。然后在第一种情况下,额外多一位就OK了。

3.题解大致为

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll t,n,i;
	cin>>t;
	while(t--){
		string x;
		cin>>x;
		ll f=-2,m=x.length();
		for(i=m-1;i>=0;i--){
			if(x[i]>='5'){
				x[i-1]++;
				f=i-1;	
			}
		}
		if(f==-2){
			cout<<x<<endl;
		}
		else if(f==-1){
			cout<<"1";
			for(i=0;i<m;i++){
				cout<<"0";
			}
			cout<<endl;
		}
		else{
			for(i=0;i<f;i++){
				cout<<x[i];
			}
			cout<<x[f];
			for(i=f+1;i<m;i++){
				cout<<"0";
			}
			cout<<endl;
		}
	}
}

4.所以只要有了思路,代码只不过是锦上添花罢了,重要的还得是在于思考问题的关键。

(二)自我感受

本周是逐渐开始有点进步的一周,上周结束之后,我的能力也逐渐的熟练了起来。这是我自己的进步,对我将来甚至做题,感觉都有着巨大的功效和益处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值