The return string -- panggo.com

Source: Internet
Author: User

The question is still from panggo.com.

Question details:
A back-to-text string is the same string from left to right and from right to left. Given a string consisting of only lowercase letters, you can rearrange its letters, to form different input strings.
Input:A non-null string consisting of lowercase letters. The length cannot exceed 100;
Output:Number of all input strings (because the result may be very large, the result of getting the remainder of 1000000007 is output ).

For example:Input "AABB" and Output 2 (because "AABB" corresponds to two input strings: Abba and Baab ).

First, let's talk about the nature of the return string. Obviously, since the left-to-right and right-to-left strings are the same, a vertical split line is made at the center of the string (the string is an even number, the vertical split line is just in the middle of two letters; the odd number, the vertical split line through the middle of the letter), then, the left and right symmetric position, the letter is the same. Therefore, if a string has an even number, each letter must have an even number. If there are an odd number of strings, except for one in the middle that has been crossed by the vertical bisector, each of the remaining letters must have an even number. If there are two or more letters in a string with an odd number, the string cannot constitute a return string. However, we only need to calculate the number of input strings that can be combined on the left (or right) of the vertical bisector. On the left side, it will be composed of half of the number of all letters (if there is an odd number of letters, the number is generally halved ). The number of return strings, that is, the number of letters that can be sorted and combined into different strings.

This is a simple question. But there are two places to talk about.

First:It is a question of how to calculate the number of input strings. Assume that the total number of letters is m, and there are n letters (n <= m) in total. There are M1 letters in X1 and M2 letters in X2. Assume that N is a total of Mn letters, then the number of input strings is M! /(M1! * M2! * ...... Mn !).

Second:Is how to calculate a relatively large number of Modulo, Java has a class is biginteger, use this class is more convenient. However, it is not actually needed. Just use the pattern operation rule:

(A + B) % P = (a % P + B % P) % P (A * B) % P = (a % P * B % P) % p I stole a lazy and used the biginteger class. The following is my source code:
Import Java. math. biginteger; public class main {// always consider overflow. Using biginteger directly would be better for public static int palindrome (string S) {// If (S = NULL | "". equals (s) {// If the string is null, 0 return 0;} // calculate int bignumber = 1000000007 in normal cases; // obtain the prime numeric value int count [] = new int [26]; // a total of 26 letters (Java automatically initializes to 0 without manual initialization) int odd = 0; // The number of letters is an odd number. Only one letter can have an odd number of int I = 0; S = S. touppercase (); char letters [] = S. tochararray (); for (I = 0; I <letters. length; ++ I) {count [letters [I]-65] ++ ;}for (I = 0; I <26; ++ I) {If (count [I] % 2 = 1) {odd ++ ;}} if (odd> 1) {// more than one letter has an odd return 0 ;} else {Int J = 0; for (I = 0; I <26; ++ I) {count [I]/= 2; // obtain half of the number} biginteger res = biginteger. valueof (1l); int n = letters. length/2; for (I = 2; I <= N; ++ I) {res = res. multiply (biginteger. valueof (I) ;}for (I = 0; I <26; ++ I) {If (count [I]> 0) {for (j = 2; j <= count [I]; ++ J) {res = res. divide (biginteger. valueof (j) ;}}res = res. moD (biginteger. valueof (long) bignumber); Return (INT) res. longvalue () ;}/ *** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub string x = "hqaymehhrsfuqrpahrimsxftuxqr?jouehaqtsryxjhearxmogmi"; system. out. println (palindrome (x ));}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.