(回文串全排列个数) xiaoxin juju needs help
題目
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?
Input
This problem has multi test cases. First line contains a single integer T(T≤20) which represents the number of test cases.
For each test case, there is a single line containing a string S(1≤length(S)≤1,000).
Output
For each test case, print an integer which is the number of watermelon candies xiaoxin’s leader needs to buy after mod 1,000,000,007.
Sample Input
3
aa
aabb
a
Sample Output
1
2
1
分析與解答
參考代碼以及思路:
https://www.cnblogs.com/shentr/p/5349489.html
https://blog.csdn.net/fun_zero/article/details/50989103
真的十分奇怪,這題我用小費馬求逆元,wrong answer。之前小費馬求出來的都不用判斷逆元的正負,我估計還是錯在逆元的正負上了,最后改也沒改出來,用歐幾里得求逆元倒是求出來了
1.回文串的性質
給你n個字母,求可以組成的回文串的個數
1.1。形成回文串的條件
1.n為奇數,有一個字母的個數為奇數
2.n為偶數,字母個數全為偶數
所以回文串中最多僅有一個字母個數為奇數
滿足這一條件才能夠形成回文串
1.2。回文串的所有組成的可能情況
設回文串的長度為n,則左邊的長度為n/2
由于回文串關于中點對稱,所以只要滿足形成回文串的條件,我們就只需找左邊的所有可能情況,如果左邊每個字母都是只出現一次,那么就是(n/2)!,但是這里可能有重復情況,那么就(n/2)!/(ni!nj! …),這里ni!中的ni是整個串中字符i出現的次數/2(因為是左邊),這個是高中有重復排列的公式
2.求模
由于有除法,所以先求逆元,轉換為乘法就ok了
總結
以上是生活随笔為你收集整理的(回文串全排列个数) xiaoxin juju needs help的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab guide对话框+滑动条+
- 下一篇: 图书管理系统_目前图书管理系统存在的问题