当前位置:   article > 正文

Codeforces Round #724 (Div. 2)_c. diluc and kaeya time limit per test2 seconds

c. diluc and kaeya time limit per test2 seconds
B. Prinzessin der Verurteilung
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
I, Fischl, Prinzessin der Verurteilung, descend upon this land by the call of fate an — Oh, you are also a traveler from another world? Very well, I grant you permission to travel with me.

It is no surprise Fischl speaks with a strange choice of words. However, this time, not even Oz, her raven friend, can interpret her expressions! Maybe you can help us understand what this young princess is saying?

You are given a string of n lowercase Latin letters, the word that Fischl just spoke. You think that the MEX of this string may help you find the meaning behind this message. The MEX of the string is defined as the shortest string that doesn't appear as a contiguous substring in the input. If multiple strings exist, the lexicographically smallest one is considered the MEX. Note that the empty substring does NOT count as a valid MEX.

A string a is lexicographically smaller than a string b if and only if one of the following holds:

a is a prefix of b, but a≠b;
in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

Find out what the MEX of the string is!

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000). Description of the test cases follows.

The first line of each test case contains an integer n (1≤n≤1000) — the length of the word. The second line for each test case contains a single string of n lowercase Latin letters.

The sum of n over all test cases will not exceed 1000.

Output
For each test case, output the MEX of the string on a new line.

Example
inputCopy
3
28
qaabzwsxedcrfvtgbyhnujmiklop
13
cleanairactbd
10
aannttoonn
outputCopy
ac
f
b

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
题意:求最短未出现串,要求字典序尽量小。
题解:考虑到1个字母有26种情况,2个字母有26*26种,3个字母有26*26*26>1000种情况,MEX最长为3。
枚举所有情况即可
  • 1
  • 2
  • 3
#include<bits/stdc++.h>

using namespace std;
vector<string>q1,q2,q3;
//分别存储1,2,3个字母

void mem(){
	for(int i='a';i<='z';++i){
		string ss="";
		ss+=(char)i;
		q1.push_back(ss);
		string s1=ss;
		for(int j='a';j<='z';++j){
			ss=s1;//使每次ss初始1个字母
			ss+=(char)j;
			q2.push_back(ss);
			string s2=ss;
			for(int k='a';k<='z';++k){
				ss=s2;//初始ss有两个字母
				ss+=char(k);
				q3.push_back(ss);
			}
		}
	}
}

void solve(){
	int n;
	string s;
	cin>>n>>s;
	for(int i=0;i<q1.size();++i){
		if(s.find(q1[i])!=string::npos) continue;
		cout<<q1[i]<<endl;
		return;
	}
	for(int i=0;i<q2.size();++i){
		if(s.find(q2[i])!=string::npos) continue;
		cout<<q2[i]<<endl;
		return;
	}
	for(int i=0;i<q3.size();++i){
		if(s.find(q3[i])!=string::npos) continue;
		cout<<q3[i]<<endl;
		return;
	}
}

int main()
{
	int T;
	cin>>T;
	mem();
	while(T--) solve();
	//for(int i=0;i<q2.size();++i) cout<<q2[i]<<"\n";
	//for(;;);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
C. Diluc and Kaeya
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The tycoon of a winery empire in Mondstadt, unmatched in every possible way. A thinker in the Knights of Favonius with an exotic appearance.

This time, the brothers are dealing with a strange piece of wood marked with their names. This plank of wood can be represented as a string of n characters. Each character is either a 'D' or a 'K'. You want to make some number of cuts (possibly 0) on this string, partitioning it into several contiguous pieces, each with length at least 1. Both brothers act with dignity, so they want to split the wood as evenly as possible. They want to know the maximum number of pieces you can split the wood into such that the ratios of the number of occurrences of 'D' to the number of occurrences of 'K' in each chunk are the same.

Kaeya, the curious thinker, is interested in the solution for multiple scenarios. He wants to know the answer for every prefix of the given string. Help him to solve this problem!

For a string we define a ratio as a:b where 'D' appears in it a times, and 'K' appears b times. Note that a or b can equal 0, but not both. Ratios a:b and c:d are considered equal if and only if a⋅d=b⋅c.

For example, for the string 'DDD' the ratio will be 3:0, for 'DKD' — 2:1, for 'DKK' — 1:2, and for 'KKKKDD' — 2:4. Note that the ratios of the latter two strings are equal to each other, but they are not equal to the ratios of the first two strings.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000). Description of the test cases follows.

The first line of each test case contains an integer n (1≤n≤5⋅105) — the length of the wood.

The second line of each test case contains a string s of length n. Every character of s will be either 'D' or 'K'.

It is guaranteed that the sum of n over all test cases does not exceed 5⋅105.

Output
For each test case, output n space separated integers. The i-th of these numbers should equal the answer for the prefix s1,s2,…,si.

Example
inputCopy
5
3
DDK
6
DDDDDD
4
DKDK
1
D
9
DKDKDDDDK
outputCopy
1 2 1 
1 2 3 4 5 6 
1 1 1 2 
1 
1 1 1 2 1 2 1 1 3 
Note
For the first test case, there is no way to partition 'D' or 'DDK' into more than one block with equal ratios of numbers of 'D' and 'K', while you can split 'DD' into 'D' and 'D'.

For the second test case, you can split each prefix of length i into i blocks 'D'.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
题意:有一个只含D,K的字符串,求其每个前缀字符串最多能被分成多少个子段,满足每个子段的D:K数量之比相同。
题解:每个字符串,有x个D,y个K,多个D:K=x:y的字符串合起来还是x:y,所以最终这个串的答案就是x:y的个数。
  • 1
  • 2
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

int gcd(int a,int b){
	if(b==0) return a;
	return gcd(b,a%b);
}

void solve(){
	int n; string s;
	cin>>n>>s;
	int d=0,k=0;
	map<pair<int,int>,ll>mp;
	for(int i=0;i<n;++i){
		if(s[i]=='D') d++;
		else k++;
		pair<int,int>idx={d/gcd(d,k),k/gcd(d,k)};
		mp[idx]++;
		cout<<mp[idx]<<" ";
	}
	puts("");
}
int main()
{
	int T;
	cin>>T;
	while(T--) solve();
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/71515
推荐阅读
相关标签
  

闽ICP备14008679号