Sunday, July 31, 2016

UVA 10338 - Mischievous Children Solution

problem link-click here

Here i see all the factorial problems at UVA are almost same. This problem is more easy bcz you just need to pre calculate  factorial till 20 and store them in an array. Then count the length of the input string and count the frequency of every character separately . then use the formula for getting how many unique ways of combination is possible which is  ( n! / multiple of repeated character's frequencies ).

an accepted code is given below:

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

vector<long long int> v;

void pre()
{
    v.push_back(1);

    for(int i=1;i<=20;i++)
    {
        v.push_back(v[i-1]*i);
    }
}

int main()
{
    pre();

    long long int t,i=1,n,s,w;
    cin>>t;

    while(t--)
    {
        char str[1000]="";
        bool arr[1000]={0};

        cin>>str;

        n=strlen(str);

        w=1;

        for(int i=0;i<strlen(str)-1;i++)
        {
            s=1;

            if(arr[str[i]]==0)
            {
                for(int j=i+1;j<strlen(str);j++)
                {
                    if(str[i]==str[j])
                    {
                        s++;
                    }
                }

                arr[str[i]]=1;
            }

           w=w*v[s];
        }

        cout<<"Data set "<<i<<": "<<v[n]/w<<endl;

        i++;
    }

    return 0;
}

No comments:

Post a Comment