Xinjiang University ACM-ICPC Program Design Competition May Month (sync) F monkey sort expectation "java/high precision/combinatorial mathematics + probability theory"

Source: Internet
Author: User

链接:https://www.nowcoder.com/acm/contest/116/F来源:牛客网题目描述 我们知道有一种神奇的排序方法叫做猴子排序,就是把待排序的数字写在卡片上,然后让猴子把卡片扔在空中,等落下的时候观察这些卡片是否从左到右已经排序完成(我们认为不会发生卡片落地后叠在一起的情况)如果有序则排序完成,否则让猴子再扔一遍,直到卡片有序,那么问题来了,给你N个卡片,每个卡片上写着一个大写字母,请问猴子第一次扔这些卡片就按字典序排序完成的概率有多大?输入描述:第一行是一个整数N(1<N<100)表示给猴子N张卡片,接下来是一个长度为N的字符串,代表这些卡片上所写的字母。输出描述:输出一行,表示猴子排序第一次就成功的概率(用分子为1的分数表示)。示例1输入7SCIENCE输出1/1260

This code found wrong, actually is the string input can not use nextline and to use next??? Me r
"Parse": the factorial of the number of occurrences of a character in a (n,n)/string in the denominator =n! /(k1! * k2! * ...)

First, next () must be read to a valid character before the end of the input, to enter a valid character before the space bar, tab or enter key, such as The Terminator, the next () method will automatically remove it, only after entering a valid character, the next () method will be entered after the space bar, The TAB key or enter key is treated as a delimiter or terminator. Simply put, next () finds and returns the next complete tag from this scanner. The complete tag is preceded by the input information that matches the delimited pattern, so the next method cannot get a string with a space and the Terminator of the Nextline () method is just the enter key, that is, the nextline () method returns all characters before the ENTER key. It is possible to get a string with a space.
Import java.util.hashmap;import java.util.map;import java.util.scanner;import java.io.*;import java.math.*;p ublic          Class main{public static BigInteger fun (int n) {BigInteger num = Biginteger.one;         for (int i=1;i<=n;i++) num=num.multiply (biginteger.valueof (i));      return num; }//7 science//1/1260 public static void Main (string[] args) {Scanner cin = new Scanner (system.in)                      ;           int n = cin.nextint (); String s = cin.nextline (); !!!!!!                      Change to Next () char[] A=s.tochararray ();                        Map<character, integer> map = new hashmap<character,integer> ();           BigInteger Pro = Fun (n);           System.out.println (PRO);           for (char c:a) {//record the number of occurrences of each character, if it does not appear initialized to 1, otherwise +1 map.put (c,!map.containskey (c)) 1: (Map.get (c) +1));               }//Traversal Map,map.get (key) is the value corresponding to the given key for (Character Key:map.keySet ()) {   SYSTEM.OUT.PRINTLN (key + "=" + map.get (key));                if (Map.get (key) >1) {Pro = Pro.divide (Map.get (key));           }} System.out.print (1);           System.out.print ("/");                   System.out.println (PRO); }    }

No map with Hash

import java.io.*;import java.util.*;import java.util.Scanner;import java.math.BigInteger;import java.lang.*;public class Main{    static BigInteger fun(int n){        BigInteger num = BigInteger.ONE;          for(int i=2; i<=n; i++)            num = num.multiply(BigInteger.valueOf(i));         return num;      }    public static void main(String[] args) {                      Scanner cin = new Scanner(System.in);                      int n = cin.nextInt();           String s = cin.next();           int[] arr = new int[500];                      for(int i=0; i<s.length(); i++) {               arr[s.charAt(i)-'A']++;           }                      BigInteger pro = fun(n);                      for(int i=0;i<500;i++) {               if(arr[i]!=0) {                   pro = pro.divide(fun(arr[i]));               }           }           System.out.println("1/"+pro);                   }    }//7 SCIENCE//1/1260

Xinjiang University ACM-ICPC Program Design Competition May Month (sync) F monkey sort expectation "java/high precision/combinatorial mathematics + probability theory"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.