Brute Force method

Source: Internet
Author: User

Brute force method is the most intuitive, the most direct, from beginning to end, from top to bottom thinking to try to solve the problem.

It mainly consists of the following three ways:

    1. One-to-one solution: bubble sort
    2. Try all possible iterations: Sequential lookup, pattern matching
    3. Try all permutations: Nearest point pair, knapsack problem
// Bubble Sort void bubble_sort (array[0,.., N]) {    for i=0 to i=n-2:               // I mean take the first few bubbles         for j=0 to j=n-2-i:                 if array[j] > array[j+1  ]:                swap (array[j], array[j+1])}
/** Pattern Matching * Direct thinking: * Brute Force method, a comparison of elements; After unsuccessful, move back one bit, continue to compare*/#include<string>using namespacestd;voidMatchpatternbyforce (stringStrstringpattern) {Str_ptr= Str.begin (), pattern_ptr =Pattern.begin ();  while(Str_ptr! ="\ n") str_ptr_tmp=str_ptr;  while(Pattern_ptr! ="\ n")            if(*str_ptr++! = *pattern_ptr++) Break; if(Pattern_ptr = ="\ n")returnstr_ptr-Str.begin (); Str_ptr++; return-1;}
/** Backpack Problem * Ideas: * Brute Force method: The total number of permutations is 2 of the N-square; by means of a bitmap representation to determine whether each element exists in a combination*/#include<vector>#include<iostream>#include<string>using namespacestd;/*Brute Force Method*/intFindfittestbybitmap (capacity, things[0, N]) {allpossbilities=2<<N;  forloop_num=0To loop_num=Allpossbilities:bit_pos=0; Sum_value=0; Max_value=0;  whileLoop_num >0: Bit_value= loop_num%2; Loop_num= Loop_num/2; ifBit_value:sum_value+=Bit_value; ifSum_value <= capacity && sum_value >Max_value:max_value=Sum_value; returnMax_value;}

Brute Force method

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.