HDU 5229 ZCC loves strings
来源:互联网 发布:js img onload 编辑:程序博客网 时间:2024/05/20 02:28
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.
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(T≤5) which denotes the number of test cases.
For each test case, there is an integerN(2≤N≤20000) 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.
For each test case, there is an integer
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
13xllendonexllendthreexllendfour
Sample Output
2/3
来个字典树判重
#include<iostream>#include<cmath>#include<map>#include<vector>#include<queue>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 100005;int T, n, m, ans, a, b, A, B, C;char s[maxn + maxn];class tire{public: tire *next[26]; int cnt; tire(){ cnt = 0; memset(next, 0, sizeof(next)); }}*root;int gcd(int x, int y){ if (x%y) return gcd(y, x%y); return y;}void insert(){ tire *j = root; for (int i = 0; s[i]; i++) { if (!j->next[s[i] - 'a']) { j->next[s[i] - 'a'] = new tire; } j = j->next[s[i] - 'a']; } j->cnt++;}int dfs(tire *x){ int tot = (x->cnt)*(x->cnt - 1) / 2; for (int i = 0; i < 26; i++) if (x->next[i]) tot += dfs(x->next[i]); return tot;}int main(){ scanf("%d", &T); while (T--) { scanf("%d", &n); a = b = A = B = 0; root = new tire; while (n--) { scanf("%s", s); insert(); if (strlen(s) & 1) a++; else b++; } B = A = a * b; A += dfs(root); if (a > 1) B += a*(a - 1) / 2; if (b > 1) B += b*(b - 1) / 2; if (A) C = gcd(A, B); if (!A) printf("0/1\n"); else printf("%d/%d\n", A / C, B / C); }}
0 0
- HDU 5229 ZCC loves strings
- hdu 5229 ZCC loves strings
- hdu 5229 ZCC loves strings
- HDU 5229 ZCC loves strings
- HDU 5229 ZCC loves strings
- hdu 5229 ZCC loves strings(博弈)
- HDU 5229 - ZCC loves strings (思维)
- HDU-5229-ZCC loves strings 【奇偶】
- hdu 5299 ZCC loves strings
- HDOJ 5229 ZCC loves strings
- hdu 5229 ZCC loves strings && BestCoder Round #41 1002
- HDU 5229 - ZCC loves strings(博弈+概率)
- hdu 5229 ZCC loves strings(Bestcoder Round #41)
- Hdu 5229 ZCC loves strings(简单题)
- HDU5229 ZCC loves strings
- HDU5229-ZCC loves strings
- hdu5229 ZCC loves strings 博弈
- hdu 4882 ZCC Loves Codefires
- Linux 软件看门狗 watchdog
- 安卓学习记录
- (推荐)Android 编程下的 TraceView 简介及其案例实战
- java中的IO整理
- C++函数指针
- HDU 5229 ZCC loves strings
- hdu 1258 Sum It Up(dfs)
- 实现字典树Trie的基本操作
- Forward&Redirect
- Hadoop之—— Linux搭建hadoop环境(简化篇)
- 详解Java中Map用法
- Ubuntu 1404 安装 TL-WN725N v2
- DOM 可被 JavaScript 用来读取、改变 HTML、XHTML 以及 XML 文档。
- 使用eclipse导出apk并为apk签名