HDOJ 5651-xiaoxin juju needs help【字符的全排列】
来源:互联网 发布:西游之路坐骑进阶数据 编辑:程序博客网 时间:2024/06/06 13:08
xiaoxin juju needs help
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1132 Accepted Submission(s): 324
Problem Description
As we all known, xiaoxin is a brilliant coder. He knew **palindromic** strings when he was only a six grade student at elementry school.
This summer he was working at Tencent as an intern. One day his leader came to ask xiaoxin for help. His leader gave him a string and he wanted xiaoxin to generate palindromic strings for him. Once xiaoxin generates a different palindromic string, his leader will give him a watermelon candy. The problem is how many candies xiaoxin's leader needs to buy?
This summer he was working at Tencent as an intern. One day his leader came to ask xiaoxin for help. His leader gave him a string and he wanted xiaoxin to generate palindromic strings for him. Once xiaoxin generates a different palindromic string, his leader will give him a watermelon candy. The problem is how many candies xiaoxin's leader needs to buy?
Input
This problem has multi test cases. First line contains a single integerT(T≤20) which represents the number of test cases.
For each test case, there is a single line containing a stringS(1≤length(S)≤1,000) .
For each test case, there is a single line containing a string
Output
For each test case, print an integer which is the number of watermelon candies xiaoxin's leader needs to buy after mod1,000,000,007 .
Sample Input
3aaaabba
Sample Output
121
Source
BestCoder Round #77 (div.2)
解题思路:
我们要找到这个字符串可以组成多少个回文串,当然可以看出字母的个数为奇数的个数超过两个包括两个就输出0.
需要推出一个公式p表示字母数量/2.
#include <cstdio>#include <cstring>#define rep(i,j,k) for(i=j;i<k;++i)typedef long long ll;const ll mod = 1000000007;int p[26]; ll a[1001];char str[1005];ll quick_pow(ll a, ll b) { ll ans = 1; for (; b; b /= 2, a = (a * a) % mod) if (b & 1) ans = (ans * a) % mod; return ans;}int main() { int t, i, n, odd; a[0] = 1; rep(i,1,1001) a[i] = (a[i - 1] * i) % mod; scanf("%d", &t); while (t--) { memset(p, 0, sizeof p); scanf("%s", str); n = strlen(str); odd = -1; rep(i,0,n) ++p[str[i] - 'a']; ll ans = a[n / 2]; rep(i,0,26) if (p[i] & 1) if (odd == -1) odd = i; else { ans = 0; goto end; } rep(i,0,26) ans = (ans * quick_pow(a[p[i] / 2], mod - 2)) % mod; end: printf("%lld\n", ans); } return 0;}
0 0
- HDOJ 5651-xiaoxin juju needs help【字符的全排列】
- hdoj 5651 xiaoxin juju needs help (排列组合)
- hdoj-5651-【xiaoxin juju needs help】
- HDU 5651xiaoxin juju needs help 带模除法逆元+有重复元素的全排列值
- hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)
- hdoj 5651 xiaoxin juju needs help 【组合数学】
- HDOJ 5651 xiaoxin juju needs help【逆元】
- xiaoxin juju needs help
- xiaoxin juju needs help
- xiaoxin juju needs help
- HDU 5651 xiaoxin juju needs help
- HDU 5651 xiaoxin juju needs help
- HDU 5651 xiaoxin juju needs help
- HDU 5651 xiaoxin juju needs help
- 杭电5651 xiaoxin juju needs help
- HDU 5651 xiaoxin juju needs help
- HDU 5651 xiaoxin juju needs help
- HDUOJ 5651 xiaoxin juju needs help
- 送伞
- 28 map
- ## BaseAdapter的优化 ##
- a1005. Spell It Right (20)
- 【HDOJ 5653】 Bomber Man wants to bomb an Array.(DP)
- HDOJ 5651-xiaoxin juju needs help【字符的全排列】
- 红黑树代码实践
- 归园田居 东晋 陶渊明 注音版
- AOJ2249Contest Page(最短路径spfa)
- 借助SystemBarTint实现透明标题栏(沉浸式)
- 最近对问题,分治法求解
- Android Studio 2.1 Preview 4 发布,以及新增的两点攺动.
- adb相关问题
- 合并模拟器和真机调试的静态库