C. Hamburgers

来源:互联网 发布:ubuntu jira部署 编辑:程序博客网 时间:2024/06/05 09:46

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) и 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "ВSCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again.


Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese.


Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient.


Input
The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C).


The second line contains three integers nb, ns, nc (1?≤?nb,?ns,?nc?≤?100) — the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1?≤?pb,?ps,?pc?≤?100) — the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1?≤?r?≤?1012) — the number of rubles Polycarpus has.


Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.


Output
Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0.


Sample test(s)
Input
BBBSSC
6 4 1
1 2 3
4
Output
2
Input
BBC
1 10 1
1 10 1
21
Output
7
Input
BSC
1 1 1
1 1 3
1000000000000
Output

200000000001


一个一个算:#include<stdio.h>int main(){    char str[200];    int a[4],val[4],b,s,c,i,buy,flagb,flags,flagc;    __int64 r,sum,price;    while(scanf("%s",str)!=EOF)    {        b=s=c=0;        for(i=0;str[i]!='\0';i++)        {            if(str[i]=='B')                b++;            else if(str[i]=='S')                s++;            else                c++;        }        for(i=1;i<=3;i++)            scanf("%d",&a[i]);        for(i=1;i<=3;i++)            scanf("%d",&val[i]);        scanf("%I64d",&r);        price=b*val[1]+s*val[2]+c*val[3];        sum=0; flagb=flags=flagc=0;        while(r)        {            if((a[1]||a[2]||a[3]))            {                if(flagb==0&&b==0)//注意这里的判断条件,很重要的。{a[1]=0;flagb=1;}else{if(a[1]>=b)a[1]-=b;else{buy=b-a[1];if(r>=buy*val[1]){r-=buy*val[1];a[1]=0;}elsebreak;}}if(flags==0&&s==0){a[2]=0;flags=1;}else{if(a[2]>=s)a[2]-=s;else{buy=s-a[2];if(r>=buy*val[2]){r-=buy*val[2];a[2]=0;}elsebreak;}}if(flagc==0&&c==0){flagc=1;a[3]=0;}else{if(a[3]>=c)a[3]-=c;else{buy=c-a[3];if(r>=buy*val[3]){r-=buy*val[3];a[3]=0;}elsebreak;}}                sum++;            }            else            {                sum+=r/price;                break;            }        }        printf("%I64d\n",sum);    }    return 0;}下面这种方法用到了两个很巧妙的地方,值得学习:#include<stdio.h>int main(){    char str[200];    int b,s,c,i,ts,tx,a[4],val[4],buy;    __int64 price ,sum,r;    while(scanf("%s",str)!=EOF)    {            b=s=c=0;            for(i=0;str[i]!='\0';i++)            {                if(str[i]=='B')                    b++;                else if(str[i]=='S')                    s++;                else                    c++;            }            for(i=1;i<=3;i++)                scanf("%d",&a[i]);            for(i=1;i<=3;i++)                scanf("%d",&val[i]);            scanf("%I64d",&r);            ts=(b>0)+(s>0)+(c>0);//关键点,很巧妙的地方            price=b*val[1]+s*val[2]+c*val[3];            sum=0;int flag=0;            while(r)            {                if((a[1]||a[2]||a[3])&&flag==0)                {                   if(a[1]>=b)                        a[1]-=b;                   else                   {                       buy=b-a[1];                       if(r>=buy*val[1])                       {                           r-=buy*val[1];                           a[1]=0;                       }                       else                        break;                   }                   if(a[2]>=s)                        a[2]-=s;                   else                   {                       buy=s-a[2];                       if(r>=buy*val[2])                       {                           r-=buy*val[2];                           a[2]=0;                       }                       else                        break;                   }                   if(a[3]>=c)                        a[3]-=c;                   else                   {                       buy=c-a[3];                       if(r>=buy*val[3])                       {                           r-=buy*val[3];                           a[3]=0;                       }                       else                        break;                   }                   sum++;                   tx=(a[1]==0)+(a[2]==0)+(a[3]==0);//关键点,很巧妙的地方                   if(tx==ts)                    flag=1;                }                else                {                    sum+=r/price;                    break;                }            }            printf("%I64d\n",sum);    }    return 0;}


0 0
原创粉丝点击