class Rotation {
public:
bool chkRotation(string A, int lena, string B, int lenb) {
if(lena != lenb)
return false;
string str_target = A + A;
if(KMP_Find(str_target, B))
return true;
else
return false;
}
bool KMP_Find(string& s,string& t)
{
int i=0,j=0;
int n =t.size();
int *next = new int[n];
GetNext(t,next);
while(i<s.size()&&j<t.size())
{
if((j == 0)||(s.at(i)==t.at(j)))
{
i++;
j++;
}else{
j = next[j];
}
}
if(j==t.size())
{
//int index=i-t.size()+1;
//cout<<"子串从长串的第"<<index<<"位开始匹配成功!"<<endl;
return true;
}
//cout<<"字符串匹配失败!"<<endl;
return false;
}
void GetNext(string& s,int *next)
{
int len = s.size();
next[0] = 0;
next[1] = 0;
int i = 1;
while(i < len - 1)
{
int j = next[i];
while(j > 0 && s.at(j) != s.at(i)) j = next[j];
if(s.at(j) == s.at(i)) next[i + 1] = j + 1;
else next[i + 1] = j;
i++;
}
}
};