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