Wednesday, May 25, 2016

12626 - I ❤ Pizza - Solution hints

https://uva.onlinejudge.org/external/126/12626.pdf

This is an easy string processing problem . just think for sometimes regarding the problem and i guess you will find a way to solve it but if you don't then don't worry i'm here for giving you logic hints for solving this problem . ok let's start : 


1 . Here it is said that you have to determine how many MARGARITA pizza you can get from a given string . So 1st of all you need to know how many words in "MARGARITA" word without repetition .

2 .  Here M 1 time , A 3 times , R 2 times , G 1 time, I 1 time , T 1 time . store the word "MARGIT" into a string and store the corresponding letter frequency "1,3,2,1,1,1" into a integer array . 

3 . Now check how many times the character "M,A,R,G,I,T" exists in given string . Then just divide the new frequency by the old for corresponding letter and again store in a new integer array . 

4 . Now just sort the new array and print the 1st element as the after dividing the frequency the minimum result will be the answer because all the other elements frequency can satisfy the minimum resultant one . 

An accepted code is given below : 


#include<bits/stdc++.h>
using namespace std;

int main()
{
    string p="MARGTI";
    string line;
    int arr[10]={1,3,2,1,1,1};
    int t,i,j,s,d,k;

    scanf("%d",&t);

    while(t>0)
    {
        int arr1[1000]={};
        s=0;

        cin>>line;

        for(i=0;i<p.length();i++)
        {
            d=0;

            for(j=0;j<line.length();j++)
            {
                if(p[i]==line[j])
                {
                    d++;
                }
            }

            arr1[s++]=d/(arr[i]);

        }

        sort(arr1,arr1+s);

        printf("%d\n",arr1[0]);

        t--;
    }

    return 0;

}

No comments:

Post a Comment