String left-right spin--three-step rotation method and phase shift method

Source: Internet
Author: User

Title: Implements a function that can be left-handed in a string of K characters.
Aabcd left-handed one character gets ABCDA
Aabcd left two characters get Bcdaa

Method 1:3-Step rotation method

Left-handed program thinking: first, according to drawing know left-handed results, and then in the analysis of the implementation of the simple steps;
First step: string all reverse order;
The second step: the left string in reverse order;
Step three: Reverse the string on the right;

Example: L-k=1 characters:

The first is the header file and the main function section:

#define _crt_secure_no_warnings 1#include<stdio.h> #include <windows.h> #include <assert.h># Include<string.h>int Main () {char arr[] = "AABCD";/* undefined array contents cannot be enclosed in {}, only in the form of "". */printf ("%s\n", My_left_move (arr, 1));/* Indicates a left-handed character */system ("pause"); return 0;}

Next is the left-hand function Implementation section:

char* my_left_move (  char *src, int count)/* The implementation of the function, where SRC is a pointer variable, cannot be added Coonst constant property, is a non-modifiable lvalue */{int i = 0;int len = strlen (src) char tmp = 0;assert (SRC);//assert the validity of the pointer for (i = 0; i < LEN/2; i++)//First step: The whole reverse order {takes an array way: and the loop condition is divided by 2 to optimize the algorithm;/*tmp = src[i];src[ I] = src[i + len-1];src[i + len-1] = tmp;*/in the form of a pointer conforming to function definition     tmp=* (src+i); * (src + i) = * (src  + len-1-i);//The subscript here is to go by yourself The analysis of SRC becomes src+len-1,+i-i; * (src + len-1-i) = tmp;} for (i = 0; i < (len-count)/2; i++)//second step: the left string in reverse order {tmp = * (src + i); * (src + i) = * (src + len-count-1-i); * (src + len -Count-1-i) = tmp;}  for (i=0; I <count/2; i++)//Third step: the right string in reverse order {tmp =* (src + len-count+i); * (src + len-count  + i) = * (Src+len-1-i); * (src + Len-1-i) = tmp;} return SRC;}


Then the whole program: (left-hand one character)

L-One character: #define _crt_secure_no_warnings 1#include<stdio.h> #include <windows.h> #include <assert.h> #include <string.h>char* my_left_move (char *src, int count)/* The implementation of the function, where SRC is a pointer variable, cannot be added Coonst constant property, is a non-modifiable lvalue */{int i = 0 ; int len = strlen (src); char tmp = 0;assert (SRC);//Assert pointer validity for (i = 0; i < LEN/2; i++)//First step: whole reverse order {takes an array: and the loop condition is divided by 2 to optimize /*tmp = Src[i];src[i] = src[i + len-1];src[i + len-1] = tmp;*/in the form of pointers conforming to function definitions tmp=* (src+i); * (src + i) = * (src + len-1-i);//Here the subscript to analyze the SRC itself becomes src+len-1,+i-i; * (src + len-1-i) = tmp;} for (i = 0; i < (len-count)/2; i++)//second step: the left string in reverse order {tmp = * (src + i); * (src + i) = * (src + len-count-1-i); * (src + len -Count-1-i) = tmp;} for (i=0; I <count/2; i++)//Third step: the right string in reverse order {tmp =* (src + len-count+i); * (src + len-count + i) = * (Src+len-1-i); * (src + Len-1-i) = tmp;} return SRC;} int main () {char arr[] = "AABCD";/* undefined array contents cannot be enclosed in {}, only in the form of "". */printf ("%s\n", My_left_move (arr, 1));/* Indicates a left-handed character */system ("pause"); return 0;}


Example: L-k=2 characters:

Regardless of the number of left-handed characters, the same way, just to distinguish between the left and right strings when the string to be moved as a whole, its internal order unchanged;

The same first header file and main function:

#define _crt_secure_no_warnings 1#include<stdio.h> #include <windows.h> #include <assert.h># Include<string.h>int Main () {char arr[] = "AABCD";/* undefined array contents cannot be enclosed in {}, only in the form of "". */printf ("%s\n", My_left_move (arr, 2));/* Indicates a two-character left-hand */system ("pause"); return 0;}


Next is the left-hand function Implementation section:

char* My_left_move (char *src, int count)/* The implementation of the function, where SRC is a pointer variable, cannot be added Coonst constant property, is a non-modifiable lvalue */{int i = 0;int len = strlen (src); char TMP = 0;assert (SRC);//assert the validity of the pointer for (i = 0; i < LEN/2; i++)//First step: The whole reverse order {takes the form of an array: and the loop condition is divided by 2 to optimize the algorithm;/*tmp = Src[i];src[i] = src[i + len-1];src[i + len-1] = tmp;*/takes the form of a pointer to the function definition tmp = * (src + i), * (src + i) = * (src + len-1-i);//The subscript here itself to analyze the src change Became a src+len-1,+i into a-i;* (src + len-1-i) = tmp;}  for (i = 0; i < (len-count)/2; i++)//second step: the left string in reverse order {tmp = * (src + i); * (src + i) = * (src + len-count-1-i); * (SRC) len-count-1-i) = tmp;} for (i = 0; I <count/2; i++)//Third step: the right string in reverse order {tmp = * (src + len-count + i); * (src + len-count + i) = * (src + len-2 -i); * (src + len-2-i) = tmp;} return SRC;}



Then the whole program: (two characters left)

#define _crt_secure_no_warnings 1#include<stdio.h> #include <windows.h> #include <assert.h># include<string.h>char* My_left_move (char *src, int count)/* The implementation of the function, where SRC is a pointer variable, cannot be added Coonst constant property, is a non-modifiable lvalue */{int i = 0; int len = strlen (src), char tmp = 0;assert (SRC),//Assert pointer validity for (i = 0; i < LEN/2; i++)//First step: whole reverse order {takes array: and the loop condition is divided by 2 for optimal  /*tmp = Src[i];src[i] = src[i + len-1];src[i + len-1] = tmp;*/in the form of a pointer conforming function definition TMP = * (src + i); * (src + i) = * (src + len- 1-I);//The subscript here itself to analyze the src becomes src+len-1,+i into-i;* (src + len-1-i) = tmp;}  for (i = 0; i < (len-count)/2; i++)//second step: the left string in reverse order {tmp = * (src + i); * (src + i) = * (src + len-count-1-i); * (SRC) len-count-1-i) = tmp;} for (i = 0; I <count/2; i++)//Third step: the right string in reverse order {tmp = * (src + len-count + i); * (src + len-count + i) = * (src + len-2 -i); * (src + len-2-i) = tmp;} return SRC;} int main () {char arr[] = "AABCD";/* undefined array contents cannot be enclosed in {}, only in the form of "". */printf ("%s\n", My_left_move (arr, 2));/* Indicates a two-character left-hand */system ("pause"); return 0;}

Method Two: Phase Shift method

We can move the opposite rotation by drawing the way:

We directly do the k=2 characters:

01.<span style= "Font-size:18px;color: #ff0000;" ><strong> 02.</strong></span> #define _crt_secure_no_warnings 1 03. #include <assert.h> 04. #include <windows.h> 05. #include <stdio.h> 06. #include <string.h> 07. void My_left_move (char* str, int count) 08.  {in. assert (str);  int i = 0;  one. int len = strlen (str);  if (Len <= 1) 13.  {. return;  15.} 16.  for (i = 0; I <count; i++) 17.  {. int j = 0;  . Char tmp = str[0]; <span style= "Font-size:18px;color: #ff0000;"  ><strong>//Save the first character </strong></span> 20.   for (j = 0; J < Len-1; J + +) 21.    {22.23.  * (str + j) = * (str + j + 1); <span style= "FONT-SIZE:18PX;" ><span style= "color: #ff0000;"   ><strong>//then pan </strong> 24.</span></span>} 25.  * (str + len-1) = tmp; <span style= "color: #ff0000;" ><strong><span style= "FONT-SIZE:18PX;" >//the contents of the TMP save to the last empty space &Lt;/span></strong> 26.</span>} 27. } 28.int Main () 29.  {. Char arr[] = "AABCD";  . int k= 2;  My_left_move (arr, k);  printf ("%s\n", arr);  System ("pause");  return 0;     36.}



Summary: The only difference between a single character or multiple characters is the number of reverse of the string on the right of the last step, and the cyclic condition K is the count change!

So do you want to do a right-handed? In fact, you only need to draw a picture to know that the right and left-hand is the same thing is only you in the distinction between the left and right string starting position is different!

So here's a right-handed implementation of a k=count character: (using three-step rotation method)

(appropriate k for the reference)

The phase shift method can also be used here!!!

<strong >char* My_right_move (char *src, int count)/* The implementation of the function, where SRC is a pointer variable, cannot be added Coonst constant property, is a non-modifiable lvalue */{int i = 0;int len = strlen (src) char tmp = 0;assert (SRC);//assert the validity of the pointer for (i = 0; i < LEN/2; i++)//First step: The whole reverse order {takes an array way: and the loop condition is divided by 2 to optimize the algorithm;/*tmp = SRC[I];SR C[i] = src[i + len-1];src[i + len-1] = tmp;*/takes the form of a pointer to the function definition tmp = * (src + i); * (src + i) = * (src + len-1-i);//The subscript here should be analyzed by itself The SRC becomes the src+len-1,+i and becomes the-i;* (src + len-1-i) = tmp;}  for (i = 0; i < (len-count)/2; i++)//second step: the left string in reverse order {tmp = * (src + i); * (src + i) = * (src + len-count-1-i); * (SRC) Len-count-1-i) = tmp;} for (i = 0; I <count/2; i++)//Third step: the right string in reverse order {tmp = * (src + len-count + i); * (src + len-count + i) = * (src + len-1 -i); * (src + len-1-i) = tmp;} return SRC;} </strong> 

Title: A string that determines whether a string is rotated after another string.
For example: given S1 = AABCD and S2 = Bcdaa, return 1, given S1 = ABCD and S2 = ACBD, return 0.


Aabcd left-handed one character gets ABCDA
Aabcd left two characters get Bcdaa

AABCD right-handed one character to get DAABC
AABCD right two characters to get Cdaab

Also first the head file and the main function are well-prepared:

#define _crt_secure_no_warnings 1.  #include <assert.h>  #include <windows.h>  19.# Include<stdio.h>  . #include <string.h>  .  22.int Main ()  23.{  .    char arr[] = "AABCD";    int ret = My_move (arr, "abcda");    if (ret = = 1)  .    {  .        printf ("yes\n");  .    }    else  .    {  .        printf ("no\n");  .    System ("pause");    return 0;  37.  

Then the function implementation section:

Char My_move (char* str1, char* str2)  42.{    assert (STR1);  A.    assert (STR2);  A.    int len = strlen (str1);    strncat (str1, str1, Len);    if (strstr (str1, str2))  .    {  .        return 1;  .    else  .    {  .        return 0;  55.}  

Finally, the whole program:

#define _crt_secure_no_warnings 1.  #include <assert.h>  . #include <windows.h>  04.# include<stdio.h>  #include <string.h>  06.char my_move (char* str1, char* str2)  07.{    assert (STR1);    (STR2);  Ten.    int len = strlen (str1);    strncat (str1, str1, Len);    if (strstr (str1, str2))  .    {  .        return 1;  .    else  .    {  .        return 0;  .}  21.int Main ()  22.{  .    char arr[] = "AABCD";  .    int ret = My_move (arr, "abcda");    if (ret = = 1)  .    {  .        printf ("yes\n");  .    else  .    {  .        printf ("no\n");  .    System ("pause");    return 0;  .}         . <span style= "color: #cc0000;" >  </span>

Haha, to here, the whole left-right spin believe already at a glance, continue to refuel it!



String left-right spin--three-step rotation method and phase shift 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.