String HDU - 6572

Avin has a string. He would like to uniform-randomly select four characters (selecting the same character is allowed) from it. You are asked to calculate the probability of the four characters being ”avin” in order.
Input
The first line contains n (1 ≤ n ≤ 100), the length of the string. The second line contains the string. To simplify the problem, the characters of the string are from ’a’, ’v’, ’i’, ’n’.
Output
Print the reduced fraction (the greatest common divisor of the numerator and denominator is 1), representing the probability. If the answer is 0, you should output “0/1”.
Sample Input
4
avin
4
aaaa
Sample Output
1/256
0/1

题意:

给你一个由 a v i n 四个字符组成的字符串,从字符串中任意取四个字符,排列这四个字符,能组成avin的概率为多大;(每个字符可以重复使用)

思路:

总方案数:
一共四个位置;每个位置可以有n种放法(假设字符串长度为n);结果为 sum = n * n * n * n
成功方案数:
统计出avin四个字符的出现的次数 a, b, c, d;结果为cot = a * b * c * d;

最终结果: cot (gcd) / sum(gcd);
注意特判情况;

#include 
using namespace std;
map<char,int> mp;
int main()
{
    int n;
    char ss;
    scanf("%d",&n);
    getchar();
    for(int i=0;i<n;i++)
    {
        scanf("%c",&ss);
        mp[ss]++;
    }
    n=n*n*n*n;
    int sum=1;
    sum=mp['a']*mp['v']*mp['i']*mp['n'];
    if(sum==0) printf("0/%d\n",n);
    else
    {
        int gd=__gcd(sum,n);
        sum=sum/gd;
        n=n/gd;
        printf("%d/%d\n",sum,n);
    }
    
    return 0;
}

你可能感兴趣的:(思维,基础算法——数论)