Last active
July 7, 2020 04:22
-
-
Save nguyenkhoa0721/6ba2c2959aa7f1ee15aca143cf239a40 to your computer and use it in GitHub Desktop.
HASH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<bits/stdc++.h> | |
| using namespace std; | |
| const long long base=31,mod=1000000003; | |
| long long hashP[1000006],hashC,POW[1000006],lenP,lenC; | |
| string strC,strP; | |
| long long gethash(int l,int r){ | |
| if (l==1){ | |
| return hashP[r]; | |
| } | |
| return (hashP[r]-hashP[l-1]*POW[r-l+1]+mod*mod)%mod; | |
| } | |
| int main(){ | |
| //freopen("test.inp","r",stdin); | |
| //freopen("test.out","w",stdout); | |
| cin>>strP; | |
| cin>>strC; | |
| lenP=strP.size(); | |
| lenC=strC.size(); | |
| strP=" "+strP; | |
| strC=" "+strC; | |
| for (int i=1;i<=lenC;i++){ | |
| hashC=(hashC*base+strC[i]-'a'+1)%mod; | |
| } | |
| POW[1]=base; | |
| for (int i=2;i<=lenP;i++){ | |
| POW[i]=(POW[i-1]*base)%mod; | |
| } | |
| for (int i=1;i<=lenP;i++){ | |
| hashP[i]=(hashP[i-1]*base+strP[i]-'a'+1)%mod; | |
| } | |
| for (int i=lenC;i<=lenP;i++){ | |
| long long tmp=gethash(i-lenC+1,i); | |
| if (hashC==tmp){ | |
| cout<<i-lenC+1<<" "; | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment