Chapter II: Rotate, modified words

Source: Internet
Author: User
Tags greatest common divisor

1. Vector rotation

A one-dimensional vector with n elements is left-handed, I-bit.

1.1 Use of I extra space

voidLeft_rotate (string&s,inti) {    stringS2 (s),0, i);//Copy the first I character to a S2    intj=0; //shifts the remaining n-i elements to the left of I position     for(; I<s.size (); i++) {s[j++] =S[i]; }    //Copy the S2 to the back part of S     for(intk=0; K<s2.size (); k++) {s[j++] =S2[k]; }}

1.2 Defines a function to turn the vector to the left 1 bits, and then I call the function

//left-handed onevoidOne_left_rotate (string&s) {    CharCH = s[0];  for(intI=0; I<s.size ()-1; i++) {S[i]= s[i+1]; } s[s.size ()-1] =ch;} //I call the L-once functionvoidLeft_rotate (string&s,inti) {     for(intj=0; j<i;j++) {one_left_rotate (s); }}

1.3 Transpose the front I bit first, then transpose the n-i bit, and finally transpose the whole vector

void left_rotate (string &s,int  i) {    reverse (s.begin (), S.begin ()+i);  the reverse () function does not access the element referred to in the latter iterator, it needs to #include<algorithm>    reverse (s.begin () +i,s.end ());    Reverse (S.begin (), S.end ());}

1.4 Acrobatics algorithm

voidLeft_rotate3 (string&s,introtdist) {    intn =s.size (); intm =gcd (n,rotdist);  for(intI=0; i<m;i++){        intt=S[i]; intj =i;  while(true){            intK = j+rotdist; if(k>=N) {k-=N; }            if(k==i) {                 Break; } S[j]=S[k]; J=K; } S[j]=T; }} 

GCD (M,n) is the greatest common divisor of M and N. There are several implementation methods:

//1. Divide and seek greatest common divisor, n is 0 terminateintgcdintMintN) {//M>n must be guaranteed    intR;  while(n!=0) {R= m%N; M=N; N=R; }    returnm;}//2. Recursive implementation of the Division of the DivideintgcdintMintN) {//M>n must be guaranteed    returnn==0? M:GCD (n,m%n);}//3. More subtractive damage, m==n terminationintgcdintMintN) {     while(m!=N) {        if(m>N) {m-=N; }        Else{n-=m; }    }     returnm;} 

1.5-Block Switching algorithm

//swap S[a. A+m-1] with S[b. B+M-1]voidSwapstring&s,intAintBintm) {     for(intI=0; i<m;i++) {Swap (S[a+i],s[b+i]); }}//Block Exchange AlgorithmvoidLeft_totate (string&s,introtdist) {    intn =s.size (); Rotdist= rotdist%n;//rotdist>=n, simply rotate the rotdist%n bit    if(rotdist==0){        return; }    inti =rotdist; intp =rotdist; intj = n-p;//Number of elements in right     while(I! =j) {        if(i>j) {Swap (S,p-i,p,j); I-=J; }        Else{swap (s,p-i,p+j-i,i); J-=i; }} swap (S,p-i,p,i);} 

Rotate in 1.6STL ()

DefinedinchHeader <algorithm>Template<classForwardIt >voidRotate (forwardit First, ForwardIt N_first, ForwardIt last); (until C + + One) Template<classForwardIt >ForwardIt Rotate (forwardit First, ForwardIt N_first, ForwardIt last); (Since C++ One) Parametersfirst-The beginning of the original Rangen_first-The element that should appear at the beginning of the rotated Rangelast-The end of the original Rangereturn value (none) (Until C++ One) The iterator equal to first+ (Last-n_first) (since C + + One)//point to previous first element after rotate//Simple rotation to the leftRotate (V.begin (), V.begin () +1, V.end ()); //Simple rotation to the rightRotate (V.rbegin (), V.rbegin () +1, V.rend ());

1.7 Development

The left-I position is equivalent to the right-handed n-i.

2. Inflection words

Given an English dictionary, find out all the modified parts of speech.

    1. Signature: Alphabetical order of letters in a word as signatures
    2. Sort: Sort a word by signature
    3. Squeeze: Output words with the same signature as lines

#include <iostream>#include<map>#include<string>#include<algorithm>using namespacestd;intMain () {Multimap<string,string>A; stringS,s0;  while(cin>>s) {S0=s;        Sort (S.begin (), S.end ());    A.insert ({s,s0}); }    stringLastkey = A.begin ()First ;  for(Auto Iter=a.begin (); ITER! = A.end (); + +ITER) {        if(Iter->first! =Lastkey) {cout<<Endl; } cout<<iter->second<<ends; Lastkey= iter->First ; }    return 0;}

3. Answer to question 6th

    1. Signature: The signature of the button encoded as the name
    2. Sort: Sort names by signature (button code)

Query using binary search Find the button code query, the button code corresponding to the name of all output.

4. Two points find and sort

The most obvious use of sorting is to sort the elements, which can be used as part of the system specification or as a preparation condition for a binary lookup program.

Two-point lookup algorithm:

//Loop ImplementationintBinary_search (Constvector<int> &iv,intkey) {    intLow =0; intHigh =iv.size ();  while(Low <=High ) {        intMid = low+ (high-low)/2; if(Key = =Iv[mid]) {            returnmid; }        Else if(key<Iv[mid]) { High= mid-1; }        Else{ Low= mid+1; }    }    return-1;//The queried keyword does not exist}//Recursive ImplementationintBinary_search (Constvector<int> &iv,intLowintHighintkey) {   intMID = low + (high-low)/2;//Do no use (low + high)/2 which might encounter overflow issue   if(Low >High )return-1; Else     {       if(Iv[mid] = =key)returnmid; Else if(Iv[mid] >key)returnBinary_search (iv, Low, mid-1, key); Else           returnBinary_search (iv, mid+1, high, key); }}

Chapter II: Rotate, modified words

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: 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.