HDU-5229-ZCC loves strings 【奇偶】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5229



ZCC loves strings




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
 

题目分析:

ZCC从n个字符串里随机拿2个字符串,Miss G.先进行操作:

1.在两个串中选择一个当前非空的串,然后在这个串的末尾删去一个字符。
2.若当前两个串完全相同且非空,则两个串都被清空。
不能再操作的人就输了,求先手的获胜概率。

特殊:如果两个字符串相同,先手获胜。
否则 先手只需每次都先取字符串较短的字符串,不会出现两个串相同的情况。删字符会影响奇偶性,如果两个字符串的长度和为奇数那么先手必胜。

那么先手胜的计算方法是
两字符串的长度和为奇数的种类(奇数串个数*偶数串个数)+两字符串相同的种类(【每种串出现k次】即为k*(k-1)/2)。   还有总的种类为n*(n-1)/2.


#include<iostream>
#include<algorithm>
using namespace std;
int gcd(int x,int y)//求最大公约数 ,约分 
{
	if(x>y)
	return gcd(y,x);
	while(y%x)
	{
		int t=x;
		x=y%x;
		y=t;
	} 
	return x;
}
int main()
{
	int T,n,a,b,i;
	string str[20002];
	cin>>T;
	while(T--)
	{
		a=b=0;
		cin>>n;
		for(i=0;i<n;i++)
		{
			cin>>str[i];
			if(str[i].length()%2)
			a++;
			else
			b++;
		}
		sort(str,str+n);
		int sum=n*(n-1)/2;
		int cnt=a*b;	
		int c=1;
		for(i=1;i<n;i++)
		{
			if(str[i]==str[i-1])
			c++;
			else
			{
				cnt+=c*(c-1)/2;//在cnt基础上+字符串相同的种类 
				c=1;
			}
		}
		cnt+=c*(c-1)/2;//最后剩余的c也要加上去 
		cout<<cnt/gcd(cnt,sum)<<"/"<<sum/gcd(cnt,sum)<<endl;		
	}
	return 0;
 } 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值