#include <iostream>
using namespace std;
bool RKalgorithm(string pat,string s){
int patL=pat.length();
int q=101;
int pat_hash=0,str_hash=0;
for(int i=0;pat[i];i++){
pat_hash+=(pat[i]-'a')%q;
}
for(int i=0;i<patL;i++){
str_hash+=(s[i]-'a')%q;
}
if(pat_hash==str_hash){
if(s.substr(0,patL)==pat)
return true;
}
int x=0;
for(int i=patL;i<s.length();i++){
str_hash-=(s[x]-'a')%q;
str_hash+=(s[i]-'a')%q;
x++;
cout<<x<<" "<<i<<endl;
cout<<s.substr(x,i)<<endl; //When i try to print it is giving the odd results
}
return false;
}
bool isRotatedStrOfs1(string s1,string s2){
string temp=s2+s2;
return RKalgorithm(s1,temp);
}
int main(){
string s1,s2;
cin>>s1>>s2;
bool res=isRotatedStrOfs1(s1,s2);
if(res)
cout<<"Strings are rotations of each other"<<endl;
else
cout<<"Strings are not rotations of each other"<<endl;
return 0;
}
INPUT:aacd acda
OUPUT:
1 4
CDAA
2 5
daacd //为什么它给我长度为5的子字符串?
3 6
aacda //为什么它给我长度为5的子串?
4 7
ACDA
字符串不是彼此的旋转
任何人都可以帮助我吗? 提前谢谢。