Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cmath>
- using namespace std;
- int Hash1(string item){
- int rez=0;
- for( char c : item){
- rez+=c-'a'+1;
- }
- return rez;
- }
- int Hash(string item){
- int rez=0;
- for(int i=0;i<item.size();i++){
- rez+=(item[i]-'a')*pow(26,i);
- }
- return rez %((int)pow(10,9)+7);
- }
- void update_hash1(int& val, char old,char nov){
- val=val-old+nov;
- }
- void update_hash(int& val, char old,char nov,int len){
- val-=old-'a';
- val/=26;
- val+=(nov-'a')*pow(26,len-1);
- val%=((int)pow(10,9)+7);
- }
- int main(){
- string str,item;
- cin>>str;
- cin>>item;
- int item_val=Hash(item);
- int val=0;
- cout<<"Item value : "<<item_val<<endl;
- for (int i=0;i<=(int)str.size()-(int)item.size();i++){
- if(i!=0){
- update_hash(val,str[i-1],str[i+item.size()-1],item.size());
- }
- else{
- val=Hash(str.substr(0, item.size()));
- }
- cout<<val<<endl;
- if(val==item_val){
- bool same=true;
- for(int j=0;j<item.size();j++){
- if(item[j]!=str[i+j]){
- same=false;
- break;
- }
- }
- if(same){
- cout<<"Found at "<<i<<endl;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement