Training Contest 5 H - H to O

题目
C6H12O6+6O2→6CO2+6H2O
Professor Cesium has created a new process to transform some chemical product into another type of chemical with some residues. The process is simple: he just needs to input a given number of molecules of type A, enter the output type B he desires and start the machine. It creates as many molecules of type B as possible. Unfortunately, professor Cadmium was jealous of his work and tried to sabotage the machine by inverting wires on his machine. Professor Cesium, alerted by one of his assistants, was able to repair the mistake. To detect any problem in the future, he is asking you to create an automatic way to compute the number of molecules that the machine should output. With this algorithm, he is able to detect whether his precious machine was tampered with.

Molecules are written as strings composed of uppercase letters (A–Z) and numbers. Uppercase letters represent atoms. Note that Cesium only uses single letters of the alphabet as abbreviations for atoms, so H, C, A, X, Y, …can be used but He, Mg, …cannot. If a letter is not followed by a number, it means there is only one atom of it. An atom followed by a number l (1≤l<103) represents l copies of that atom. Atoms can appear multiple times in a chemical product.

For example: H2OC100H means 2 atoms of H, then 1 of O, then 100 of C then 1 of H again.

Input
The first line contains the input molecule, a string of length at most 2500, followed by an integer 1≤k≤103, denoting how many of these molecules professor Cesium has.

The second line contains the desired output molecule, given as a string of length at most 2500.

Output
The output consists of a single line containing the maximum number n of output molecules we are able to construct using the input molecules.

这个题题意是你有k个所给分子数,你通过将这个分子拆分再组合成另一个分子,问你最多能组合几个你想要的分子,可考虑先统计原分子中个原子个数再与新分子中各个原子比较即可,说白了就是统计字母出现次数,然后至少满足新分子中出现的字母数量和原分子出现字母数量相除之和的最小的那个,这里考虑一种情况当新分子中出现了原分子中没有的原子则直接输出0

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
string s1,s2;
int a[50],b[50],k;
ll minn=999999999;
int main()
{
	cin>>s1>>k;
	cin>>s2;
	int len=s1.size();
	for(int i=0;i<len;i++){
		if((s1[i]>='A'&&s1[i]<='Z'&&s1[i+1]>='A'&&s1[i+1]<='Z')||(i==len-1)){   //当两个字母相连时还要记录前面字母出现了一次以及当有字母出现在最后一个位置是的情况
			a[s1[i]-'A']++;                     //通过 -'A'的操作间接记录各个字母出现次数
		}
		int num=s1[i]-'A';                
		if(s1[i]>='A'&&s1[i]<='Z'&&s1[i+1]>='0'&&s1[i+1]<='9'){
			int d=0;
			for(i++;s1[i]>='0'&&s1[i]<='9';i++) d=d*10+s1[i]-'0';
			a[num]+=d;
			i--;
		}
	}
	int len2=s2.size();
	for(int i=0;i<len2;i++){
		if((s2[i]>='A'&&s2[i]<='Z'&&s2[i+1]>='A'&&s2[i+1]<='Z')||(i==len2-1)){
			b[s2[i]-'A']++;
		}
		int num=s2[i]-'A';
		if(s2[i]>='A'&&s2[i]<='Z'&&s2[i+1]>='0'&&s2[i+1]<='9'){
			int d=0;
			for(i++;s2[i]>='0'&&s2[i]<='9';i++) d=d*10+s2[i]-'0';
			b[num]+=d;
			i--;
		}
	}
	for(int i=0;i<30;i++){
		if(b[i]&&!a[i]){
			cout<<"0"<<endl;
			return 0;
		}
		if(a[i]&&b[i]){
			if(minn>a[i]*k/b[i])//记得乘以k,minn取最小的那个值
				minn=a[i]*k/b[i];
		}
	}
	cout<<minn<<endl;
	return 0;
}
AtCoder Practice Contest #B - インタラクティブ練習 (Interactive Sorting) 是一道比较有趣的题目。它是一道交互式的排序题目,需要你与一个神秘程序进行交互,以便将一串无序的数字序列排序。 具体来说,这个神秘程序会给你一个长度为 $N$ 的数字序列,然后你需要通过询问它两个数字的大小关系,来逐步确定这个序列的排序顺序。每次询问之后,神秘程序都会告诉你两个数字的大小关系,比如第一个数字比第二个数字小,或者第二个数字比第一个数字小。你需要根据这个信息,来调整这个数字序列的顺序,然后再向神秘程序询问下一对数字的大小关系,以此类推,直到这个数字序列被完全排序为止。 在这个过程中,你需要注意以下几点: 1. 你最多只能向神秘程序询问 $Q$ 次。如果超过了这个次数,那么你的程序会被判定为错误。 2. 在每次询问之后,你需要及时更新数字序列的顺序。具体来说,如果神秘程序告诉你第 $i$ 个数字比第 $j$ 个数字小,那么你需要将这两个数字交换位置,以确保数字序列的顺序是正确的。如果你没有及时更新数字序列的顺序,那么你的程序也会被判定为错误。 3. 在询问的过程中,你需要注意避免重复询问。具体来说,如果你已经询问过第 $i$ 个数字和第 $j$ 个数字的大小关系了,那么你就不需要再次询问第 $j$ 个数字和第 $i$ 个数字的大小关系,因为它们的大小关系已经被确定了。 4. 在排序完成之后,你需要将排序结果按照从小到大的顺序输出。如果你输出的结果不正确,那么你的程序也会被判定为错误。 总的来说,这道题目需要你熟练掌握交互式程序设计的技巧,以及排序算法的实现方法。如果你能够熟练掌握这些技巧,那么就可以顺利地完成这道非传统题了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值