HDU 5229 - ZCC loves strings (思维)

题意

ZCC有N个字符串,他正在和Miss G.用这N个字符串玩一个小游戏。ZCC会从这N个串中等概率随机选两个字符串(不可以是同一个)。然后,ZCC和Miss G.轮流操作。Miss G.总是先操作的。在每轮中,操作者可以选择操作A或操作B。
操作A:在两个串中选择一个当前非空的串,然后在这个串的末尾删去一个字符。
操作B: 若当前两个串完全相同且非空,则可以使用这个操作。此时两个串都被清空。
不能操作的玩家输掉了这个游戏。
ZCC想要知道他输掉游戏的概率是多少(也就是Miss G.获胜的概率)。

思路

可耻地引用一下官方题解。

结论:对于串a和b,游戏中先手必胜当且仅当|a|+|b|为奇数或a=b.
我们按|a|+|b|的大小从小到大考虑所有的情况。
当|a|+|b|=0时,显然先手必败,符合结论。
假设已经证明了|a|+|b|=k的所有情况满足结论,现在考虑|a|+|b|=p的情况。
若p是奇数,先手只需要选择长度较短的不为空的串,并使用A操作,就可以转移到|a|+|b|为偶数并且两个串不相等或者两个串均为空的情况,这种情况先手必败,故此时先手必胜。
若p是偶数,如果两个串相等,显然先手只需要选择使用B操作就能获得胜利了。否则,无论先手如何操作,都只能转移到|a|+|b|为奇数的先手必胜的情况。故此时先手必败。
因此,按顺序考虑每一个串,求得在其之前出现的串中,长度奇偶性与其不同的串共有x个,与其完全相同的串有y个,则对答案有x+y的贡献。累加即可。

今天的BC爆零了。。蛋疼

代码

#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <string>
#include <map>
#include <cmath>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define FOR(i, a, b) for (int i=(a); (i) < (b); (i)++)
#define FOOR(i, a, b) for (int i = (a); (i)<=(b); (i)++)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
const int MAXN = 10;
const int MOD = 10000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int seed = 131;
int cases = 0;
typedef pair<int, int> pii;

map<string, int> mp;
int len[2];

int main()
{
    //ROP;
    int T;
    scanf("%d", &T);
    while (T--)
    {
        MS(len, 0);
        mp.clear();
        int n;
        scanf("%d", &n);
        LL ans = 0;
        string str;
        FOR(i, 0, n)
        {
            cin >> str;
            ans += mp[str] + len[(SZ(str)&1)^1];    //len[0]表示偶数串的个数,1是奇数串的个数
            mp[str]++;
            len[SZ(str)&1]++;
        }
        LL y = n*(n-1)/2;
        LL gcd = __gcd(ans, y);
        ans /= gcd, y /= gcd;
        printf("%I64d/%I64d\n", ans, y);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值