Find a string combination

Source: Internet
Author: User

For example, the combination of "abc" strings is a, B, c, AB, ac, bc, and abc. Here I use two methods: Bit operation:

# Include <iostream> # include <vector> using namespace std; // bit operation algorithm void main () {char * data = "abcd"; int length = strlen (data ); int last = (1 <length)-1; cout <last <endl; int I; for (I = 1; I <= last; I ++) {int currentIndex = length-1; while (currentIndex> = 0) {if (I & (1 <currentIndex) {cout <data [length-currentIndex-1];} -- currentIndex;} cout <endl ;}}

 

Recursive operation:
void algorithm(char* data,int num,vector<char>& result)  {      if(!num)      {          vector<char>::iterator it=result.begin();          for(it;it!=result.end();++it)          {              cout<<*it;          }          cout<<endl;          return;      }      if(*data=='\0')          return;      result.push_back(*data);      algorithm(data+1,num-1,result);      result.pop_back();      algorithm(data+1,num,result);  }    void main()  {      char* data="abcd";      int length=strlen(data);      vector<char> result;      int i;      for(i=1;i<=length;i++)      {          algorithm(data,i,result);      }  }  

 


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.