An analysis of the algorithm of permutation and combination query

Source: Internet
Author: User
Tags integer reset stringbuffer

The so-called permutation combination query is equivalent to Google advanced query "contains all the following words" query, that is, the query must contain all the query keywords, and their order can be arbitrary. The following program segment implements this functionality. For example, enter the query keyword: Tom tina the most common scenario is to use a "select sex from student where name like '%tom%tina% ' or '"%tina%tom% ' ordered By-age query statement to achieve the above query, so how to get '%tina%tom% ' and '%tom%tina% ' is the program and algorithm to achieve.

The first thing to think of is to write an algorithm for permutation and combination, then use the algorithm to output all the query keywords, such as I entered the following keywords: EGG apple time to write a program to output the 3 words in all permutations, such as: EGG Apple time 2 EGG T IME Apple, case 3 apple EGG time ... Needless to say, we can see that the factorial of 3 is the case that 1*2*3 is not listed here.

Write a program, or a function such as: public string Paileizuhe (string inputstr) {...} The function returns an array of query strings, such as using the function and assigning him two string parameters (Tom, Tina) Then: Public String Pailiezuhe ("Tom", "Tina"); output: "Select sex from student where name like '%tom%tina% ' or name like '%ti na%tom% ' ordered by age ' here, what we care about is how to generate Tom Tina's combination of '%tina%tom% ' and '%tom%tina% '. As for the generation of the entire string is very simple, just use StringBuffer to hang those constants up to the end of the combination. The following program shows the implementation of the permutation combination output:

import Java.math.BigInteger;


import java.util.*;


public class Permutationgenerator {


private int[] A;


private BigInteger Numleft;


private BigInteger Total;


public Permutationgenerator (int n) {


if (n < 1) {


throw new IllegalArgumentException ("Min 1");


}


a = new int[n];


total = getfactorial (n);


Reset ();


}


//------


//Reset


//------


public void Reset () {


for (int i = 0; i < a.length; i++) {


A[i] = i;


}


numleft = new BigInteger (total.tostring ());


}


//------------------------------------------------


//Return number of permutations not yet generated


//------------------------------------------------


public BigInteger Getnumleft () {


return numleft;


}


//------------------------------------


//Return total number of permutations


//------------------------------------


public BigInteger gettotal () {


return total;


}


//-----------------------------


//Are there more permutations?


//-----------------------------


public boolean Hasmore () {


return Numleft.compareto (biginteger.zero) = = 1;


}


//------------------


//Compute factorial


//------------------


private static BigInteger getfactorial (int n) {


BigInteger fact = Biginteger.one;


for (int i = n; i > 1; i--) {


fact = fact.multiply (New BigInteger (integer.tostring (i)));


}


return fact;


}


//--------------------------------------------------------


//Generate next permutation (algorithm from Rosen p. 284)


//--------------------------------------------------------


public int[] GetNext () {


if (numleft.equals (total)) {


numleft = numleft.subtract (Biginteger.one);


return A;


}


int temp;


//Find largest index J with A[j] < a[j+1]


int j = a.length-2;


while (A[j] > a[j + 1]) {


j--;


}


//Find index k such this a[k] is smallest integer


//greater than a[j] to the right of A[j]


int k = a.length-1;


while (A[j] > A[k]) {


k--;


}


//Interchange A[j] and A[k]


temp = a[k];


A[k] = a[j];


A[j] = temp;


Put tail end of permutation after jth position in increasing order


int r = a.length-1;


int s = j + 1;


while (R > s) {


temp = A[s];


A[s] = A[r];


A[r] = temp;


r--;


s++;


}


numleft = numleft.subtract (Biginteger.one);


return A;


}


//program test entrance


public static void Main (string[] args) {


int[] indices;


string[] elements = {"1", "2", "3"};


permutationgenerator x = new Permutationgenerator (elements.length);


StringBuffer permutation;


while (X.hasmore ()) {


permutation = new StringBuffer ("%");


indices = X.getnext ();


for (int i = 0; i < indices.length; i++) {


permutation.append (Elements[indices[i]). Append ("%");


}


System.out.println (permutation.tostring ());


}


}


}

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.