Judge whether string s2 can be obtained through s1 cyclic shift

Source: Internet
Author: User

<1> Method 1: cyclically traverse [cpp] # include <iostream> # include <string> using namespace std; bool IsSubstring (char * src, char * des) {int len = strlen (src); for (int I = 0; I <len; I ++) {if (strstr (src, des )! = NULL) return true; char tempchar = src [0]; for (int j = 0; j <len-1; j ++) src [j] = src [j + 1]; src [len-1] = tempchar;} return false;} void main () {char src [20] = "abc", des [20] = "bcd"; if (IsSubstring (src, des) cout <"OK" <endl; else cout <"NO" <endl; strcpy (src, "abcdef"), strcpy (des, "bc"); if (IsSubstring (src, des )) cout <"OK" <endl; else cout <"NO" <endl;}/* no OK Summary: 1. Note that in IsSubstring, even c Har src [] Still degrades to a pointer. If you use a String constant, Error 2 will occur. Even if it is char *, you can still use the strlen function, because it judges '\ 0', sizeof 3 cannot be used after it degrades to a pointer, and char [] is used as a global variable, note that the size of the allocated memory has been determined, when using strcpy in the future, be sure not to cross-border arrays */# include <iostream> # include <string> using namespace std; bool IsSubstring (char * src, char * des) {int len = strlen (src); for (int I = 0; I <len; I ++) {if (strstr (src, des )! = NULL) return true; char tempchar = src [0]; for (int j = 0; j <len-1; j ++) src [j] = src [j + 1]; src [len-1] = tempchar;} return false;} void main () {char src [20] = "abc", des [20] = "bcd"; if (IsSubstring (src, des) cout <"OK" <endl; elsecout <"NO" <endl; strcpy (src, "abcdef"), strcpy (des, "bc"); if (IsSubstring (src, des )) cout <"OK" <endl; elsecout <"NO" <endl;}/* NOOK Conclusion: 1. Note that in IsSubstring, even if it is char src [], it is still degraded as a pointer, you If you use a String constant, Error 2 will occur. Even if it is char *, you can still use the strlen function because it judges '\ 0'. After it degrades to a pointer, sizeof3 cannot be used, when using the char [] global variable, note that the size of the allocated memory has been determined. When using strcpy, note that the array is out of bounds */Method 2: replace string s1 with s1s1, check whether s2 is s1s1 substring [cpp] # include <iostream> # include <string> using namespace std; bool IsSubstring (char * src, char * des) {char * srcTemp = new char [strlen (src) + 1]; strcpy (srcTemp, src); srcTemp [strlen (src)] = '\ 0 '; src = strcat (src, srcTemp); if (strstr (src, des )! = NULL) {delete [] srcTemp; return true;} else {delete [] srcTemp; return false ;}} void main () {char src [20] = "abcdef ", des [20] = "bcdg"; if (IsSubstring (src, des) cout <"OK" <endl; else cout <"NO" <endl; strcpy (src, "abcdef"), strcpy (des, "bc"); if (IsSubstring (src, des) cout <"OK" <endl; else cout <"NO" <endl;}/* no OK Summary: 1, char * strstr (const char * string, const char * strCharSet); char str [] = "Lazy"; char string [] = "The quick brown dog jumps over the lazy fox"; char * pdest = strstr (string, str ); then pdest = lazy fox; 2. For strcat, there is a section on MSDN that No overflow checking is saved med when strings are copied or appended. the behavior of strcat is undefined if the source and destination strings overlap. that is to say, the source string and the target string cannot overlap; otherwise, an error occurs. Therefore, strcat (s, s) is undefined */# include <iostream> # include <string> using namespace std; bool IsSubstring (char * src, char * des) {char * srcTemp = new char [strlen (src) + 1]; strcpy (srcTemp, src); srcTemp [strlen (src)] = '\ 0 '; src = strcat (src, srcTemp); if (strstr (src, des )! = NULL) {delete [] srcTemp; return true;} else {delete [] srcTemp; return false ;}} void main () {char src [20] = "abcdef ", des [20] = "bcdg"; if (IsSubstring (src, des) cout <"OK" <endl; elsecout <"NO" <endl; strcpy (src, "abcdef"), strcpy (des, "bc"); if (IsSubstring (src, des) cout <"OK" <endl; elsecout <"NO" <endl;}/* NOOK conclusion: 1, char * strstr (const char * string, const char * strCharSet ); char str [] = "lazy"; char string [] = "The quick brown dog jumps over the lazy fox"; char * pdest = strstr (string, str); then pdest = lazy fox; 2, about strcat, no overflow checking is saved med when strings are copied or appended. the behavior of strcat is undefined if the source and destination strings overlap. that is to say, the source string and the target string cannot overlap; otherwise, an error occurs. Therefore, strcat (s, s) is undefined */<2> Custom strcat MyStrcat. The reason for decryption of src and des memory cannot overlap. [Cpp] # include <iostream> # include <string> # include <cassert> using namespace std; /// // char * MyStrcat (char * src, char * des) {assert (src! = NULL & des! = NULL); int srcEnd = 0; while (1) {if (src [srcEnd]! = '\ 0') srcEnd ++; else break;} char * pdes = des; while (1) {if (* pdes! = '\ 0') {src [srcEnd] = * pdes; srcEnd ++; pdes ++;} else {src [srcEnd] =' \ 0'; break ;}} return src ;} /// // bool IsSubstring (char * src, char * des) {char * srcTemp = new char [strlen (src) + 1]; strcpy (srcTemp, src); srcTemp [strlen (src)] = '\ 0 '; src = MyStrcat (src, srcTemp); if (strstr (src, des )! = NULL) {delete [] srcTemp; return true;} else {delete [] srcTemp; return false ;}} void main () {char src [20] = "abcdef ", des [20] = "bcdg"; if (IsSubstring (src, des) cout <"OK" <endl; else cout <"NO" <endl; strcpy (src, "abcdef"), strcpy (des, "bc"); if (IsSubstring (src, des) cout <"OK" <endl; else cout <"NO" <endl;}/* no OK conclusion: we can see that when the src and des memory are the same, because src has been growing, the pointer pdes pointing to des does not know where to end, causing an error. */# Include <iostream> # include <string> # include <cassert> using namespace std; /// // char * MyStrcat (char * src, char * des) {assert (src! = NULL & des! = NULL); int srcEnd = 0; while (1) {if (src [srcEnd]! = '\ 0') srcEnd ++; elsebreak;} char * pdes = des; while (1) {if (* pdes! = '\ 0') {src [srcEnd] = * pdes; srcEnd ++; pdes ++;} else {src [srcEnd] =' \ 0'; break ;}} return src ;} /// // bool IsSubstring (char * src, char * des) {char * srcTemp = new char [strlen (src) + 1]; strcpy (srcTemp, src); srcTemp [strlen (src)] = '\ 0 '; src = MyStrcat (src, srcTemp); if (strstr (src, des )! = NULL) {delete [] srcTemp; return true;} else {delete [] srcTemp; return false ;}} void main () {char src [20] = "abcdef ", des [20] = "bcdg"; if (IsSubstring (src, des) cout <"OK" <endl; elsecout <"NO" <endl; strcpy (src, "abcdef"), strcpy (des, "bc"); if (IsSubstring (src, des) cout <"OK" <endl; elsecout <"NO" <endl;}/* NOOK conclusion: we can see that when the src and des memory are the same, because src has been growing, the pointer pdes pointing to des does not know where to end, causing an error. */

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.