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 <bits/stdc++.h>
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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值