寒假集训(一)

01 [AtCoder-abc336_a] Long Loong

Problem Statement

For a positive integer X, the Dragon String of level X is a string of length (X+3) formed by one L, X occurrences of o, one n, and one g arranged in this order.

You are given a positive integer N. Print the Dragon String of level N.
Note that uppercase and lowercase letters are distinguished.

Constraints

1≤N≤2024
N is an integer

Solution

模拟即可

Programming

点击查看代码
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	cout<<"L";
	for(int i=1;i<=n;i++)
		cout<<"o";
	cout<<"ng";
	return 0;
}

02 [CF1703A] YES or YES?

Problem Statement

There is a string s of length 3, consisting of uppercase and lowercase English letters. Check if it is equal to "YES" (without quotes), where each letter can be in any case. For example, "yES", "Yes", "yes" are all allowable.

Input

The first line of the input contains an integer t (1≤t≤10^3) — the number of testcases.

The description of each test consists of one line containing one string ss consisting of three characters. Each character of ss is either an uppercase or lowercase English letter.

Output

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

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

Solution

使用c++内置函数toupper将字符串全部转成大写字母,再判断字符串是否与YES相同即可

Programming

点击查看代码
#include<bits/stdc++.h>
using namespace std;
int main(){
	int t=1;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		for(auto &x:s)x=toupper(x);
			if(s=="YES")cout<<"YES\n";
			else cout<<"NO\n";
	}
	return 0;
}

03 [洛谷P2955] Even? Odd? G

Description

Bessie’s cruel second grade teacher has assigned a list of N (1 <= N <= 100) positive integers I (1 <= I <= 10^60) for which Bessie must determine their parity (explained in second grade as ‘Even… or odd?’). Bessie is overwhelmed by the size of the list and by the size of the numbers. After all, she only learned to count recently.

Write a program to read in the N integers and print ‘even’ on a single line for even numbers and likewise ‘odd’ for odd numbers.

POINTS: 25

Bessie那惨无人道的二年级老师搞了一个有 N 个正整数 I 的表叫Bessie去判断“奇偶性”(这个词语意思向二年级的学生解释,就是“这个数是单数,还是双数啊?”)。Bessie被那个表的长度深深地震惊到了,竟然跟栋栋的泛做表格一样多道题!!!毕竟她才刚刚学会数数啊。

写一个程序读入N个整数,如果是双数,那么在单立的一行内输出"even",如果是单数则类似地输出"odd".

数据范围:每个正整数不超过 10^60。

Input

  • Line 1: A single integer: N

  • Lines 2…N+1: Line j+1 contains I_j, the j-th integer to determine even/odd

Output

  • Lines 1…N: Line j contains the word ‘even’ or ‘odd’, depending on the parity of I_j

Solution

使用python模拟即可

Programming

点击查看代码
t = int(input())
while t:
    x = int(input())
    if x % 2 == 1:
        print("odd")
    else :
        print("even")
    t = t - 1

04 [CF1980A] Problem Generator

Vlad is planning to hold mm rounds next month. Each round should contain one problem of difficulty levels 'A', 'B', 'C', 'D', 'E', 'F', and 'G'.

Vlad already has a bank of nn problems, where the ii-th problem has a difficulty level of ai. There may not be enough of these problems, so he may have to come up with a few more problems.

Vlad wants to come up with as few problems as possible, so he asks you to find the minimum number of problems he needs to come up with in order to hold mm rounds.

For example, if m=1n=10, a= 'BGECDCBDED', then he needs to come up with two problems: one of difficulty level 'A' and one of difficulty level 'F'.

Input

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

The first line of each test case contains two integers nn and m (1≤n≤50, 1≤m≤51≤m≤5) — the number of problems in the bank and the number of upcoming rounds, respectively.

The second line of each test case contains a string aa of nn characters from 'A' to 'G' — the difficulties of the problems in the bank.

Output

For each test case, output a single integer — the minimum number of problems that need to come up with to hold mm rounds.

Solution

首先我们可以先计算出m天所需要的总共的字母个数,每种字母各有m个,然后统计一下我们已经有的每个字母的个数记为mp[i],所以还需要的字母个数即为\(7m-\sum_1^nmin(mp[i],m)\),注意特判如果这个值小于等于0什么已经够了,输出0即可

Programming

点击查看代码
#include<bits/stdc++.h>
using namespace std;
int main(){
	int t=1;
	cin>>t;
	while(t--){
		int n,m;
		cin>>n>>m;
		map<char,int>mp;
		char c;
		for(int i=1;i<=n;i++){
			cin>>c;
			mp[c]++;

		}
		int sum=7*m;
		for(char i='A';i<='G';i++)
			sum-=min(mp[i],m);
		cout<<max(sum,0)<<"\n";
	}
	return 0;
}

05 [洛谷B4012] rules

Background

从前有个荣光的王国,小 A 是其中的国王,他认为一个国家除了法律外还要有一些约定俗成的规则,所以今天他要赐以其规则。

Description

小 A 制定了一些规则,每条规则有一个代号,代号为不超过 10^9 的非负整数。

小 A 的国家有 n 位居民,每位居民每天会且仅会遵守 1 条规则。小 A 记录了 m 天里每天每位居民遵守的规则代号。

现在小 A 想要考察代号为 k 的规则是否符合民意,具体考察方法如下:

如果在某一天里,有大于等于一半的人遵守了规则 k,那么小 A 认为在这一天规则 k 是符合民意的。
如果在大于等于一半的天数里,规则 k 符合民意,那么他会认为规则 k 是正确的。否则,他会认为规则 k 是错误的。
如果小 A 的规则 k 是正确的,请你输出 YES,否则请你输出 NO。

Input

第一行三个整数 n,m,k,分别表示居民总数、记录的天数和小 A 想要考察的规则的代号。

接下来 m 行,每行 n 个整数分别表示每个人分别遵守的规则代号。

Output

一行一个字符串 YES 或 NO 表示小 A 的规则 k 是否是被视作正确的。

Solution
模拟即可,注意这里大于等于一半应该是上取整

Programming

点击查看代码
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,m,k;
	cin>>n>>m>>k;
	int t=0;
	for(int i=1;i<=m;i++){
		int tot=0;
		for(int j=1;j<=n;j++){
			int x;
			cin>>x;
			if(x==k)tot++;
		}
		if(tot>=(n+1)/2)t++;
	}
	if(t>=(m+1)/2)cout<<"YES";
	else cout<<"NO";
}

06 [AtCoder - abc342_c] Many Replacement

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

You will perform an operation Q times on the string S. The ii-th operation (1≤i≤Q) is represented by a pair of characters (ci,di), which corresponds to the following operation:

Replace all occurrences of the character ci in S with the character di.
Print the string S after all operations are completed.

Constraints

1≤N≤2×10^5
S is a string of length N consisting of lowercase English letters.
1≤Q≤2×10^5
ci and di are lowercase English letters (1≤i≤Q).
N and Q are integers.

Solution

暴力显然超时,因此我们考虑如何保存信息从而快速查询。第一眼感觉像个并查集,但是发现样例就能hack掉,故换个想法。我们直接记录原来字串中那些字母要被替换成哪个字母,一开始所有字母都要被替换成自己,每出现一个替换关系,比如把a替换成b,我们就寻找原来要替换成a的字母,将其替换成b,最后输出时在原字母中进行替换即可

Programming

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+7;
int n,q,a[maxn],fa[26];
char s[maxn];
map<char,char> mp;
int main(){
	cin>>n;
	for(int i=0;i<26;i++)
	fa[i]=i;
	for(int i=1;i<=n;i++)
	cin>>s[i],a[i]=(int)(s[i]-'a');
	cin>>q;
	for(int i=0;i<26;i++)
	mp[i+'a']=(char)(i+'a');
	for(int i=1;i<=q;i++){
		char c,d;
		cin>>c;
		cin>>d;
		for(int i=0;i<26;i++){
			if(mp[i+'a']==c)
			mp[i+'a']=d;
		}
//	for(int i=1;i<=n;i++)
//	cout<<mp[s[i]]<<" ";
}
	for(int i=1;i<=n;i++)
	cout<<mp[s[i]];
	return 0;
}

07 [洛谷B3977] 更好的交换

Description
小 S 有一个奇怪的机关拼图。这个拼图可以看作一个 n 行 n 列的方阵 A,第 i 行第 j 列的位置上有一个正整数 Ai,j。

与寻常拼图不同的是,这个机关拼图上的数字不能随意移动,必须按照如下规则之一操作:

选择拼图上的第 x 行和第 y 行,交换这两行;
选择拼图上的第 x 列和第 y 列,交换这两列。
为了复原这个拼图,小 S 将会操作共 m 次,每次操作格式如下:

1 x y,表示交换第 x 行和第 y 行;
0 x y,表示交换第 x 列和第 y 列;
请你输出复原后的拼图。

Input

第一行,两个正整数 n 和 m,分别表示拼图的行数、列数和总操作次数。
接下来 n 行,每行 n 个正整数 Ai,j,表示拼图上第 i 行,第 j 列上的数字。
接下来 m 行,每行三个正整数 op,x,y其中 op 表示操作类型,x,y代表被操作的行号或列号。

Output

输出共 n 行,每行 n 个正整数,表示复原后的拼图。

Solution

暴力显然超时,容易想到我们对于行和列的操作是独立的,不会互相影响,因此我们可以对于操作1和操作2单独进行求解,实际上,我们并不需要真的去交换每一行。考虑用一个数组来表示当前第i行里所存储的是初始的哪一行。那么,对于一次交换x行和y行的操作,我们只需要交换px和py即可,对于列也是同理,最后输出答案即可。

Programming

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1007;
int n,m,a[maxn][maxn],p[maxn],q[maxn];
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			cin>>a[i][j];
	for(int i=1;i<=n;i++)
		p[i]=q[i]=i;
	for(int i=1;i<=m;i++){
		int opt,x,y;
		cin>>opt>>x>>y;
		if(opt==0)swap(p[x],p[y]);
		else swap(q[x],q[y]);
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cout<<a[q[i]][p[j]]<<" ";
		}
		cout<<endl;
	}
}

总结

多思考,多练习,才能进步

posted @ 2025-01-21 23:03  isletfall  阅读(105)  评论(0)    收藏  举报