zoukankan      html  css  js  c++  java
  • BestCoder Round #41 1002

    Problem Description
    ZCC has got N strings. He is now playing a game with Miss G.. ZCC will pick up two strings among those N strings randomly(A string can't be chosen twice). Each string has the same probability to be chosen. Then ZCC and Miss G. play in turns. Miss G. always plays first. In each turn, the player can choose operation A or B.
      
    Operation A: choose a non-empty string between two strings, and delete a single letter at the end of the string.
        
    Operation B: When two strings are the same and not empty, empty both two strings.
      
    The player who can't choose a valid operation loses the game.
      
    ZCC wants to know what the probability of losing the game(i.e. Miss G. wins the game) is.
     
    Input
    The first line contains an integer T(T5) which denotes the number of test cases.
      
    For each test case, there is an integer N(2N20000) in the first line. In the next N lines, there is a single string which only contains lowercase letters. It's guaranteed that the total length of strings will not exceed 200000.
     
    Output
    For each test case, output an irreducible fraction "p/q" which is the answer. If the answer equals to 1, output "1/1" while output "0/1" when the answer is 0.
     
    Sample Input
    1 3 xllendone xllendthree xllendfour
     
    Sample Output
    2/3
     
    每次一个人有两种选择,无法操作为败,求先手胜的概率。
    由题意得,b操作对于双方玩家都是必胜操作,所以每个玩家都会避免对方出现b操作,而且每个玩家也的确有能力不让对方出现操作b的状态,因此,b操作要么先手
    第一手出现,要么不会出现。
    因此,对于第一个玩家,给定两个字符串,当且仅当两串相等或两串长度之和为奇数是胜,边读入边记录奇数长度和偶数长度串的个数,在维护一个vis map用来查询
    当前读入字符串之前出现过几次,则当前读入字符串对于概率的贡献为与其奇偶行不同的字符串个数加上当前串之前出现过的次数,对贡献求和,奇数为1 + 2 + 3 + 。。。。 + (n - 1)
    约分的答案;
    附代码:
     1 /*140MS*/
     2 #include<iostream>
     3 #include<string>
     4 #include<map>
     5 #include<algorithm>
     6 #include<cstring>
     7 #define rep(i, n) for(int i = 0 ; i < n ; i++)
     8 using namespace std;
     9 string s; map<string, int> vis;
    10 int gcd(int a, int b) {
    11     if(a < b) swap(a, b);
    12     return (b == 0)?a:gcd(b, a % b);
    13 }
    14 int main()
    15 {ios::sync_with_stdio(false);
    16     int T; cin >> T;
    17     while(T--) {
    18         int n; cin >> n;
    19         int ans = 0;
    20         int toteven = 0; int totodd = 0; vis.clear();
    21         rep(i ,n) {
    22             cin >> s;
    23             if(s.length() % 2 == 1) {
    24                 ans += toteven; if(vis.count(s)) ans += vis[s];
    25                 if(!vis.count(s)) vis[s] = 0; vis[s]++;
    26                 totodd++;
    27             }
    28             else {
    29                 ans += totodd; if(vis.count(s)) ans += vis[s];
    30                 if(!vis.count(s)) vis[s] = 0; vis[s]++;
    31                 toteven++;
    32             }
    33         }
    34         if(ans == 0) cout << "0/1" << endl;
    35         else {
    36             int b = (n * (n - 1)) / 2;
    37             int t = gcd(ans, b);
    38             cout << ans / t << "/" << b / t << endl;
    39         }
    40     }
    41     return 0;
    42 }
     
  • 相关阅读:
    [Everyday Mathematics]20150226
    [Everyday Mathematics]20150225
    [Everyday Mathematics]20150224
    [Everyday Mathematics]20150223
    [Everyday Mathematics]20150222
    [Everyday Mathematics]20150221
    [Everyday Mathematics]20150220
    [Everyday Mathematics]20150219
    [Everyday Mathematics]20150218
    [Everyday Mathematic]20150217
  • 原文地址:https://www.cnblogs.com/tooyoungtoosimple/p/4521111.html
Copyright © 2011-2022 走看看