hdu 5229 ZCC loves strings

ZCC loves strings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 622    Accepted Submission(s): 216


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
 

Source
题目分析:
这是一道博弈题,首先假设选出了两个串a和串b,那么这个对于先手来说,当两个串相等时,只要消掉这两个串直接获胜,如果两个串不等时,只要当前两个串的字母总数是奇数,那么我们一定能够构造出两个不相等的且字母数为偶数的串,那么对方一定不能赢,且无论采取何种策略,都会导致先手回到必胜态
那么对于这些串如果两两的比较,n^2的复杂度一定超时,那么我们可以换个思路,利用map存下当前i之前的串中字母数分别为奇偶的串的个数,再记录下之前的串中为某一个串时的个数,那么这样选的总方案是n*(n-1)/2个,将获胜方案比上总方案数,利用gcd化简一下即可
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <string>
#include <map>

using namespace std;

map<string,int> mp;
map<int,int> dic;
string s;

int t,n;

int gcd ( int a , int b )
{
    return b == 0 ? a : gcd ( b , a%b );
}

int main ( )
{
    scanf ( "%d" , &t );
    while ( t-- )
    {
        mp.clear();
        dic.clear();
        scanf ( "%d" , &n );
        int ans = 0;
        for ( int i = 0 ; i < n ; i++ )
        {
            cin >> s;
            if ( s.length()%2 == 1 ) 
            {
                ans += dic[1];
                dic[1]++;
            }
            else 
            {
                ans += dic[2];
                dic[2]++;
            }
            ans -= mp[s];
            mp[s]++;
        }
        int temp = n*(n-1)/2;
        ans = temp - ans;
       // cout << ans << " " << temp << endl;
        int d = gcd ( ans , temp );
        if ( ans == 0 )
            puts ("0/1");
        else if ( ans == temp )
            puts ("1/1");
        else 
            printf ( "%d/%d\n" , ans/d , temp/d );
    }
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值