Algorithm


 problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1279 

Adam’s parents put up a sign that says “CONGRATULATIONS”. The sign is so big that exactly one letter fits on each panel. Some of Adam’s younger cousins got bored during the reception and decided to rearrange the panels. How many unique ways can the panels be arranged (counting the original arrangement)? Input The first line of input is a single non-negative integer. It indicates the number of data sets to follow. Its value will be less than 30001. Each data set consists of a single word, in all capital letters. Each word will have at most 20 letters. There will be no spaces or other punctuation. The number of arrangements will always be able to fit into an unsigned long int. Note that 12! is the largest factorial that can fit into an unsigned long int. Output For each word, output the number of unique ways that the letters can be rearranged (counting the original arrangement). Use the format shown in Sample Output, below.

Sample Input 3 HAPPY WEDDING ADAM

Sample Output Data set 1: 60 Data set 2: 2520 Data set 3: 12

Code Examples

#1 Code Example with C Programming

Code - C Programming

 #include <bits/stdc++.h>

using namespace std;

long long factorial(int k)
{
    long long res = 1;
    for(int i=2;i < =k;i++) res *= i;
    return res;
}

int main()
{
    int n;
    string in;
    scanf("%d\n",&n);
    int tc = 1;
    while(n--) {
        getline(cin, in);
        unordered_map < char,int> cntMap;
        long long res = factorial(in.size());
        for(auto c : in) cntMap[c]++;
        for(auto& p : cntMap)
            res /= factorial(p.second);
        printf("Data set %d: %lld\n", tc++, res);
    }
} 
 
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
HAPPY
WEDDING
ADAM

Output

x
+
cmd
Data set 1: 60
Data set 2: 2520
Data set 3: 12
Advertisements

Demonstration


UVA Online Judge solution - 10338 - Mischievous Children - UVA Online Judge solution in C,C++,java

Previous
UVA Online Judge solution - 10337 - Flight Planner - UVA Online Judge solution in C,C++,java
Next
UVA Online Judge solution - 10340-All in All - UVA Online Judge solution in C,C++,java