Problems with string shift--problem-solving notes

Source: Internet
Author: User

problems with string shift--problem-solving notes


Title Description: Given the two strings S1 and S2, it is required to determine the string inclusions that can be obtained by S1 doing a cyclic shift (rotate) at S2 time. For example, given S1=AABCD and S2=cdaa, returns true; given S1=ABCD and S2=ACBD, returns false.


Solution 1: A direct method of traversing a string S1 after all shifts, each of which is judged to contain S2.

Analysis: This situation in the S1 string very long situation is not good, need to traverse the situation too much.


Solution 2: Notice that after the S1 string shift there will be a loop, and the loop is the repeated placement of the S1 string, that is, the ABCD becomes ABCDABCD after the cyclic shift, we can directly see the string after the shift contains S2. The code is as follows:


#include <iostream>
#include <string>
using namespace std; 

BOOL Rotatecontain (String str1, String str2)
{
	str1 + = str1; 
	if (Str1.find (str2)! = String::npos)
		return true; 
	else
		return false; 
}

int main ()
{
	string  str1 = "AABCD"; 
	String str1 = "BACDA"; 
	String str2 = "Cdaa"; 

	Cout<<rotatecontain (str1, str2) <<endl; 

	return 0; 
}


Reference documents:

The beauty of programming





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.