集训队第二次排位赛

string LCM

Let’s define a multiplication operation between a string a and a positive integer x: a⋅x is the string that is a result of writing x copies of a one after another. For example, “abc” ⋅ 2 = “abcabc”, “a” ⋅ 5 =“aaaaa”.

A string ais divisible by another string b if there exists an integer x such that b⋅x=a. For example, “abababab” is divisible by “ab”, but is not divisible by “ababab” or “aa”.LCM of two strings sand t (defined as LCM(s,t)) is the shortest non-empty string that is divisible by both s and t.You are given two strings s and t. Find LCM(s,t) or report that it does not exist. It can be shown that if LCM(s,t)exists, it is unique.
Input

The first line contains one integer q(1≤q≤2000) — the number of test cases.Each test case consists of two lines, containing strings sand t (1≤|s|,|t|≤2 ). Each character in each of these strings is either 'a' or 'b'.

Output

For each test case, print LCM(s,t)if it exists; otherwise, print -1. It can be shown that if LCM(s,t)exists, it is unique.

Example
Input

3
baba
ba
aa
aaa
aba
ab

Output

baba
aaaaaa
-1
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m,i,j;
	cin>>n;
	for(m=0;m<n;m++)
	{
		string s,t,aa="",bb="";
    	cin>>s>>t;
    	int ls=s.size(),lt=t.size();
    	int x=__gcd(ls,lt);//__gcd是最大公约数的函数
    	int lp=x*(ls/x)*(lt/x);
    	int t1=lp/ls,t2=lp/lt;
    	int p;
		for(p=1;p<=t1;p++)
			aa+=s;
		for(p=1;p<=t2;p++)
			bb+=t;
		if(aa==bb)
			cout<<aa<<endl;
		else
			cout<<"-1"<<endl;
	}
	return 0;
}

Even-odd-Game

During their New Year holidays, Alice and Bob play the following game using an array a of n

integers:

Players take turns, Alice moves first.
Each turn a player chooses any element and removes it from the array.
If Alice chooses even value, then she adds it to her score. If the chosen value is odd, Alice's score does not change.
Similarly, if Bob chooses odd value, then he adds it to his score. If the chosen value is even, then Bob's score does not change.
If there are no numbers left in the array, then the game ends. The player with the highest score wins. If the scores of the players are equal, then a draw is declared.

For example, if n=4and a=[5,2,7,3], then the game could go as follows (there are other options):On the first move, Alice chooses 2and get two points. Her score is now 2. The array a is now [5,7,3].On the second move, Bob chooses 5and get five points. His score is now 5. The array a is now [7,3].On the third move, Alice chooses 7
and get no points. Her score is now 2. The array a is now [3].On the last move, Bob chooses 3and get three points. His score is now 8. The array a is empty now. Since Bob has more points at the end of the game, he is the winner.
You want to find out who will win if both players play optimally. Note that there may be duplicate numbers in the array.
Input

The first line contains an integer t(1≤t≤104) — the number of test cases. Then t test cases follow.The first line of each test case contains an integer n
(1≤n≤2⋅105) — the number of elements in the array a.The next line contains nintegers a1,a2,…,an (1≤ai≤109) — the array aused to play the game.It is guaranteed that the sum of nover all test cases does not exceed 2⋅105.
Output

For each test case, output on a separate line:

    "Alice" if Alice wins with the optimal play;
    "Bob" if Bob wins with the optimal play;
    "Tie", if a tie is declared during the optimal play.

Example
Input

4
4
5 2 7 3
3
3 2 1
4
2 2 2 2
2
7 8

Output

Bob
Tie
Alice
Alice
#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long T,a[200001],i,a1=0,a2=0,n;
	cin>>T;
	for(int t=0;t<T;t++)
	{
		a1=0;
		a2=0;
		cin>>n;
		for(i=1;i<=n;i++)
			cin>>a[i];
		sort(a+1,a+1+n);
		reverse(a+1,a+n+1);//也可利用cmp直接排出降序顺序
		for(i=1;i<=n;i++)
		{
			if(i%2==1&&a[i]%2==0)
				a1+=a[i];
			else if(i%2==0&&a[i]%2==1)
				a2+=a[i];
		}
		if(a1>a2)
			cout<<"Alice"<<endl;
		else if(a1<a2)
			cout<<"Bob"<<endl;
		else
			cout<<"Tie"<<endl;
	}
	return 0;
}

piggy bank

Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.

But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it’s weight in grams.
Output
Print exactly one line of output for each test case. The line must contain the sentence “The minimum amount of money in the piggy-bank is X.” where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line “This is impossible.”.
Sample Input

3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4

Sample Output

The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.

此题是完全背包的变形

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int T,n,w[100001],s[100001],ans=0,m1,m2;
	cin>>T;
	while(T--)
	{
		int dp[100001]={0};
		cin>>m1>>m2;
		int m=m2-m1;
		cin>>n;
		for(int i=1;i<=m;i++)
			dp[i]=999999999;
		for(int i=1;i<=n;i++)
			cin>>w[i]>>s[i];
		for(int i=1;i<=n;i++)
			for(int j=s[i];j<=m;j++)
				dp[j]=min(dp[j],dp[j-s[i]]+w[i]);//因为题目要求最小,所以取min 
		if(dp[m]>=999999999)//若不能存满f[m]、f[m-s[i]]一定均为999999999 
			cout<<"This is impossible."<<endl;
		else
			cout<<"The minimum amount of money in the piggy-bank is "<<dp[m]<<"."<<endl;
	}
	return 0;
}

买粮食

 急!灾区的食物依然短缺!
为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买。
请问:你用有限的资金最多能采购多少公斤粮食呢?

后记:
人生是一个充满了变数的生命过程,天灾、人祸、病痛是我们生命历程中不可预知的威胁。
月有阴晴圆缺,人有旦夕祸福,未来对于我们而言是一个未知数。那么,我们要做的就应该是珍惜现在,感恩生活——
感谢父母,他们给予我们生命,抚养我们成人;
感谢老师,他们授给我们知识,教我们做人
感谢朋友,他们让我们感受到世界的温暖;
感谢对手,他们令我们不断进取、努力。
同样,我们也要感谢痛苦与艰辛带给我们的财富~

Input
输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(1<=n<=100, 1<=m<=100),分别表示经费的金额和大米的种类,然后是m行数据,每行包含3个数p,h和c(1<=p<=20,1<=h<=200,1<=c<=20),分别表示每袋的价格、每袋的重量以及对应种类大米的袋数。
Output
对于每组测试数据,请输出能够购买大米的最多重量,你可以假设经费买不光所有的大米,并且经费你可以不用完。每个实例的输出占一行。
Sample Input

1
8 2
2 100 4
4 100 2

Sample Output

400

此题为多重背包

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
	int T,t,N,V,f[100001]={0},num[1001],s[1001],w[1001],i,j,k,ans=0;
	cin>>T;
	for(t=0;t<T;t++)
	{
		ans=0;
		memset(f,0,sizeof(f));
		cin>>V>>N;
		for(i=1;i<=N;i++)
		{
			cin>>s[i]>>w[i]>>num[i];
			for(j=V;j>=s[i];j--)
			{
				for(k=1;k<=num[i]&&j>=k*s[i];k++)
					f[j]=max(f[j],f[j-k*s[i]]+k*w[i]);
				ans=max(ans,f[j]);//多余
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值