题目:给定俩个字符串s1和s2,要求判定s2是否能被s1做循环移位得到的字符串包含,例如,给定s1 = AABCD 和
s2 = CDAA;返回true;给定s1=ABCD和s2=ACBD,返回false.
参考代码:
#include<iostream>
bool isCircleRotate(const char *lha,const char *rha){
bool can_get = false;
if(lha != NULL && rha != NULL){
unsigned int len1 = strlen(lha);
unsigned int len2 = strlen(rha);
if(len1 >= len2){
char *arr = new char[2*len1+1];
//std::tr1::shared_ptr<char> pChar(new char[2*len1 + 1]);
strcpy(arr,lha);
strcat(arr,lha);
arr[2*len1] = ‘\0’;
if(strstr(arr,rha) != NULL)
can_get = true;
delete [] arr;
}
}
return can_get;
}
void test1(){
const char *lha = “AABCD”;
const char *rha = “CDAA”;
std::cout << isCircleRotate(lha,rha) << std::endl;
}
void test2(){
const char *lha = NULL;
const char *rha = NULL;
std::cout << isCircleRotate(lha,rha) << std::endl;
}
void test3(){
const char *lha = “ABCD”;
const char *rha = “ACBD”;
std::cout << isCircleRotate(lha,rha) << std::endl;
}