Uva 11340 - Newspaper

*** Solution with Brute Force *******

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>

using namespace std;

#define sf scanf
#define pf printf
#define mx 1000100

typedef long long LL;

struct my
{
    char c;
    LL v;
};

my ch[mx+9];

char mch[mx+9];

int main()
{
    int t;
    sf("%d\n",&t);

    while(t--)
    {
        //memset(ch,0,sizeof(ch));
        //memset(mch,0,sizeof(mch));
        int k;
        LL i;
        sf("%d\n",&k);

        for(i=0;i<k;i++)
        {
            sf("%c%lld\n",&ch[i].c,&ch[i].v);
        }

        LL m,s=0;
        sf("%lld\n",&m);

        for(i=0;i<m;i++)
        {
            gets(mch);

            LL len=strlen(mch);

            for(LL r=0;r<k;r++)
            {
                for(LL j=0;j<len;j++)
                {
                    if(ch[r].c == mch[j])
                    {
                        s+=ch[r].v;
                    }
                }
            }
        }

        pf("%0.2lf$\n", double(s / 100.0));
    }

    return 0;
}

 **** Solution With Map ****


 #include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<iostream>

using namespace std;

#define sf scanf
#define pf printf
#define mx 1000100

typedef long long LL;

char ch[mx+9];

int main()
{
    int t;
    map<char,int>mp;
    sf("%d\n",&t);

    while(t--)
    {
        mp.clear();
        int k;
        sf("%d\n",&k);
        char c;
        LL v,i;

        for(i=0;i<k;i++)
        {
            sf("%c%lld\n",&c,&v);
            mp[c]=v;
        }

        //for(i=0;i<k;i++)cout<<mp[i]<<" ";

        LL m,s=0;
        sf("%lld\n",&m);

        for(i=0;i<m;i++)
        {
            gets(ch);

            LL len = strlen(ch);

            for(LL j=0;j<len;j++)
            {
                s+=mp[ch[j]];
            }
        }

        //cout << s << endl;

        pf("%.2lf$\n",double(s/100.0));
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number