The existing ' ABCDEFGHIJKL ' 12 characters, sorting all its permutations in dictionary order, gives any set of permutations, and says the rent arrangement is the number one small in all permutations.

Source: Internet
Author: User

Topic:

The existing ' ABCDEFGHIJKL ' 12 characters, sorting all its permutations in dictionary order, gives any set of permutations, and says the rent arrangement is the number one small in all permutations.

It is said that this problem is a Baidu school recruit an algorithm problem, anyway I think I in the school when it is possible to do not come out. In the school time really should study well, I also escaped after graduation to regret the fate. But I'm going to say a little bit of positive energy,

It's not too late to learn, as long as you know. At least now I did not encounter too much difficulty in doing this problem, indicating that after graduation study still has a great effect. For my favorite programming, for my favorite algorithm, continue to work!

At the same time, I see this problem, the original text has the solution and ideas, but forgive me is a slag, I can not understand the mathematical formula

  

My idea is that if the first given is B (the string given is abbreviated to STR), then all strings starting with a will be in front of Str.

What is the number of strings that start with a? Is the 11-bit character that follows all permutations, which is the factorial of 11.

If the first bit is C, then all strings starting with a or B will be in front of STR, i.e. 2*11! (Note that the factorial of 11, not 11), and so on, can be used to know the number of strings that precede str when the first bit is any character.

Second, second, the idea is the same as the first, you can also know that the second is any character when the number of strings in front of STR, and so on, after the following characters are counted, add all the results

However, this time there is a problem, in this question 12 characters are fixed, so if the first bit is not a, is B, then a in the following characters will appear, and B will not appear again, so in the above calculation, can not be directly in the dictionary order to calculate, In the calculation of each bit, according to the order of the characters that have not yet appeared, to arrange the order of the characters do not appear, specific to the comments in the code, do not understand the place of the debug step-by-step look.

/*** existing ' ABCDEFGHIJKL ' 12 characters, sort all of their permutations in dictionary order, give any set of permutations, and say the rent arrangement is the number of small in all permutations * SYSTEM.OUT.PRINTLN (Getsortnum ("Abcdefghi Jlk ")); 1 * SYSTEM.OUT.PRINTLN (Getsortnum ("Hgebkflacdji")); 302715242 * SYSTEM.OUT.PRINTLN (Getsortnum ("Gfkedhjblcia")); 260726926*/     Public Static intgetsortnum (String value) {//record characters and non-appearing character sequences that have occurredString sort = "ABCDEFGHIJKL"; //record number of results        intres = 1;  for(intI=0;i<value.length (); i++) {            //take the characters of each of you in turn            Charc =Value.charat (i); //View the current order of the characters in all characters            intHead =Sort.lastindexof (c); //calculate the number of strings in frontRes + = ((head-i) *factorial (11-i)); //modifies the character order, swapping the currently occurring characters to the front of the character//because we don't care about the order of the characters that have already appeared, we just need to know which characters are present and the dictionary order of the characters that don't appear .//so just throw the characters in front of you.Sort =Change (sort, head); }        returnRes; }        /*** Ask for factorial *@paramI *@return     */     Public Static intFactorial (inti) {if(i==0)return0; intresult = 1;  for(; i>1;i--) {result*=i; }        returnresult; }        /*** Put string a bit to the front *@paramSTR *@paramA *@paramb *@return     */     Public StaticString change (String str,inta) {Char[] chars =Str.tochararray (); Chartemp =Chars[a];  for(inti=a;i>0;i--) {Chars[i]= Chars[i-1]; } chars[0] =temp; returnstring.valueof (chars); }    

Perhaps I am not very clear, I am a novice, multi-understanding, can look at the code, there is nothing wrong place welcome point

The existing ' ABCDEFGHIJKL ' 12 characters, sorting all its permutations in dictionary order, gives any set of permutations, and says the rent arrangement is the number one small in all permutations.

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.