什么是数字字谜这是在串回文 [英] what's number anagram which are palindromes in string
问题描述
什么字谜这是在一串回文的数目为? 例如:字符串=aaabbbb; 可能的变位字的哪些是回文abbabba,bbaaabb和bababab。 这里的问题是时间,我有大小10 ^ 9的字符串。 这是我最后的code有谁能够告诉我有什么错呢?
What is the number of anagrams which are palindromes in a string? Example : string = "aaabbbb"; Possible anagram's which are palindromes "abbabba" , "bbaaabb" and "bababab". The problem here is the time, i have string of size 10^9. here's my final code can anybody tell me what's the wrong with it ?
推荐答案
在你输入字符串的每个字母都有出现在偶数量,execpt一个字母可以出现在一个奇怪的量。这封信已经在palindron一个固定的位置。它必须是恰好在中间。让说的金额字母A,B,C,......是#A,#B,#C,...
Every letter in your input string has to appear in an even amount, execpt one letter can appear in an odd amount. This letter has a fixed position in the palindron. It has to be exactly in the middle. Lets say the amounts of the letter a,b,c,... are #a, #b, #c, ...
您只关心一半的信件,因为在palindron,上半场的后半depands。所以我们只使用一半的字母:
You only care about half of those letters, because in an palindron, the second half depands of the first half. So we only use half of the letters:
我用地板的功能,所以我估计信,出现在一个奇怪的量,正确的。
I used the floor function, so I calculate the letter, which appears in an odd amount, correct.
那么有多少排列是在上半场?这是不同的排列的情况下,所以我们得到
So how many permutations are in the first half? This is a case of distinct permutation, so we get
可能性。
有关您的例子: 字符串=aaabbbb;
For your example: string = "aaabbbb";
我们得到:#A = 3,#B = 4。因此,
We get: #a=3, #b=4. Therefore
我们拿到3 palindroms,这些都是abbabba,bbaaabb和bababab,就像你贴。
We get 3 palindroms, these are "abbabba" , "bbaaabb" and "bababab", like you posted.
所以,如果你有一个非常大的字符串:
So, if you have a very large string:
- 在计数的每个字母的金额
- 检查,如果是出现在一个奇怪的量只有1个字母。它有更多的,你不能创建palindroms。
- 使用表现公式来计算不同palindroms的数目。
这篇关于什么是数字字谜这是在串回文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!