题目
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;
}