Java programming language universal combination algorithm implementation

Source: Internet
Author: User
Tags bit set bitset alphanumeric characters

Java implements a universal combination algorithm. There is a collection similar to {31311133,33113330}. After 8 is used to get 5 combinations, other locations are replaced by non-alphanumeric characters, for example, using the * number, {3 *** 1133, *** 13330,...} is displayed ,......} A collection like {31311133,33113330} has a collection like {31311133, 33113330}. After a combination of 8 and 5, it is replaced by non-alphanumeric characters in other locations. For example, if the number * is used, {3 *** 1133, *** 13330,...} is displayed ,......} for a collection such as {3 *** 1133, *** 13330}, it is required to take 3 combinations after 5 again, and use non-alphanumeric characters in other locations, for example, if you use the * number, you can obtain a number similar to {***** 133 *,......} such a set. The following describes how to implement this requirement: first, the main idea is to convert 10 combinations into 01 combinations by scanning strings based on the information encoding principle. Second, for each numeric string, set a single thread, set a List in the single thread class to store the string to be processed (may contain the * number or not) the index position value of each number (rather than the * number) in. Once again, set BitSet to indicate whether each position is replaced by the * number to obtain a new composite string. Finally, during the scanning of the original numeric string to be processed, BitSet is operated based on the index in the configured character List, and a new combination is obtained for each BitSet. The Java language is implemented as follows: package org. shirdrn; import java. util. arrayList; import java. util. bitSet; import java. util. collection; import java. util. collections; import java. util. hashSet; import java. util. iterator; import java. util. list;/*** common combination and split classification (based on a single thread) ** can complete two functions: * 1. You can split a full numeric string into a string containing the number. * For example, if the input set {31311133,33113330} is input, the Splitter class traverses the set and creates a SplitterThread * thread for each string for processing. If the input set is a string of 2 and 1, that is, starCount = 8-2 = 6. After thread processing, the result is *** 33, *** 1 ** 3, and so on. * 2, the string set is obtained by splitting and filtering the string with the asterisk (*). Each string is combined. * For example, if the input set is 5, the string set is 1. {3 *** 1133, * ** 113330} www.2cto.com * CommonSplitter class will traverse the set and create a SplitterThread * thread for each string with the * sign for processing. If it is a combination of 2 strings and 1, that is, starCount = 8-3-2 = 3. After thread processing, it is similar to ****** 33, * ***** 1*3 and other results * @ author */public class CommonSplitter {privat E int starCount; private boolean duplicate; private Collection filteredContainer; public Collection getFilteredContainer () {return filteredContainer ;} /*** construct a Spilitter instance * @ param container input collection of strings to be processed * @ param starCount ), starCount = N-M * @ param duplicate whether to de-duplicate */public CommonSplitter (Collection container, int starCount, boolean duplicate) {this. duplicate = duplicate; this. starCou Nt = starCount; if (this. duplicate) {// select whether or not to create a container filteredContainer = Collections based on the specified deduplication. synchronizedSet (new HashSet ();} else {filteredContainer = Collections. synchronizedList (new ArrayList ();} Iterator it = container. iterator (); while (it. hasNext () {new Thread (new SplitterThread (it. next (). trim ())). start () ;}try {Thread. sleep (50);} catch (InterruptedException e) {e. printStackTrace () ;}}/*** ratio to a specified N-field The length of the game is N, and the single-type betting string is combined. * enter the single-type betting string, for example, 31311133. The combination is similar to ****** 33, * ***** 1*3 ,...... result set ** @ author */class SplitterThread implements Runnable {private char [] charArray; private int len; // Number of numeric characters List occupyIndexList = new ArrayList (); // index private List container = new ArrayList (); private BitSet startBitSet; // private BitSet endBitSet; // bit set termination state, used to control the loop public SplitterThread (S Tring string) {this. charArray = string. toCharArray (); this. len = string. replace ("*",""). length (); this. startBitSet = new BitSet (len); this. endBitSet = new BitSet (len); // initialize startBitSet. The left side is filled with the X symbol int count = 0; // for (int I = 0; iif (charArray [I]! = '*') {If (count <starCount) {this. startBitSet. set (I, true); count ++;} occupyIndexList. add (I) ;}// initialize endBit. The right side is filled with the * symbol count = 0; for (int I = string. length ()-1; I> 0; I --) {if (charArray [I]! = '*') {If (count <starCount) {this. endBitSet. set (I, true); count ++;} ccupyIndexList. add (I) ;}/// construct a combination string with * According to the start startBitSet and add it to the container char [] charArrayClone = this. charArray. clone (); for (int I = 0; iif (this. startBitSet. get (I) {charArrayClone [I] = '*' ;}} this. container. add (new String (charArrayClone);} public void run () {this. split (); synchronized (filteredContainer) {filteredContainer. addAll (this. container) ;}} p Ublic void split () {while (! This. startBitSet. equals (this. endBitSet) {int zeroCount = 0; // count the number of 0 on the left after 10: int oneCount = 0; // after 10, int pos = 0 on the left. // record the index position of 10 currently encountered. char [] charArrayClone = this. charArray. clone (); // traverse startBitSet to determine the position where 10 appears for (int I = 0; iif (! This. startBitSet. get (this. occupyIndexList. get (I) {zeroCount ++;} if (this. startBitSet. get (this. occupyIndexList. get (I ))&&! This. startBitSet. get (this. occupyIndexList. get (I + 1) {pos = I; oneCount = I-zeroCount; // change 10 to 01this. startBitSet. set (this. occupyIndexList. get (I), false); this. startBitSet. set (this. occupyIndexList. get (I + 1), true); break ;}// after 10 is encountered, all 1 on the left is moved to the leftmost int count = Math. min (zeroCount, oneCount); int startIndex = this. occupyIndexList. get (0); int endIndex = 0; if (pos> 1 & count> 0) {pos --; endIndex = this. occupyIndex List. get (pos); for (int I = 0; ithis. startBitSet. set (startIndex, true); this. startBitSet. set (endIndex, false); startIndex = this. occupyIndexList. get (I + 1); pos --; if (pos> 0) {endIndex = this. occupyIndexList. get (pos) ;}}// replace * (int I = 0; iif (this. startBitSet. get (this. occupyIndexList. get (I) {charArrayClone [this. occupyIndexList. get (I)] = '*';} this. container. add (new String (charArrayClone) ;}}} Test The following example shows how to use package org. shirdrn; import java. util. arrayList; import java. util. collection; import junit. framework. testCase; import org. shirdrn. util. goodTools; public class TestCommonSplitter extends TestCase {private CommonSplitter splitter; public void setSplitter (Collection container, int starCount, boolean duplicate) {this. splitter = new CommonSplitter (container, starCount, duplicate);} public void testSp Lliter () {Collection container = new ArrayList (); container. add ("1*10 **"); int starCount = 2; boolean duplicate = true; this. setSplitter (container, starCount, duplicate); System. out. println (this. splitter. getFilteredContainer ();} public void testSplliter3 () {Collection container = new ArrayList (); container. add ("1x10*1300 *"); int starCount = 3; boolean duplicate = true; this. setSplitter (container, starCount, Duplicate); System. out. println (this. splitter. getFilteredContainer (); assertEquals (35, this. splitter. getFilteredContainer (). size ();} public void testNoStar () {Collection container = new ArrayList (); container. add ("3110330"); int starCount = 3; boolean duplicate = true; this. setSplitter (container, starCount, duplicate); System. out. println (this. splitter. getFilteredContainer (); assertEquals (35, this. split Ter. getFilteredContainer (). size ();} public void testSplitter_8_310 () {// 8: 310 String multiSeq = "310,310,310,310,310,310,310,310"; Collection container = GoodTools. getNSingleList (multiSeq); assertEquals (6561, container. size (); int starCount = 4; boolean duplicate = false; this. setSplitter (container, starCount, duplicate); assertEquals (459270, this. splitter. getFilteredContainer (). size () ;}} the test time is about 2 seconds left Right. The above algorithm is implemented based on two conditions: the first is a complete numeric string --> A combination of numeric strings with the * sign; the second numeric string with the asterisk (*) --> on this basis, continue to combine to obtain the numeric string with the asterisk. If you use the above algorithm to implement the first condition, because the List is used to record the index, the execution speed is slightly lower, than in the previous implementation, the List is not used for processing.

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.