/ Published in: C++
This code doesn't work... Every time it sends -1 in answer.
Expand |
Embed | Plain Text
#include<cstdio> #include<cstring> using namespace std; #define P (int)1e9+7 long long hash(char *str, int x, int y){ long long tmp=0; for(int i=y; i>=x; i--){ tmp*=P; tmp+=str[i]; } return tmp; } int main(){ char str[1000]; char substr[20]; scanf("%s",str); scanf("%s",substr); int z=strlen(substr); long long hash1=hash(substr,0,z); printf("%lld\n",hash1); int len=strlen(str)-z; long long sum[len]; sum[0]=hash(str,0,z); printf("%lld\n",sum[0]); for(int i=1; i<len; i++){ sum[i]=sum[i-1]*P+str[i+z]; printf("%lld\n",sum[i]); } for(int i=0; i<len; i++){ if(sum[i]==hash1){ printf("%d\n",i); return 0; } } printf("-1\n"); return 0; }
You need to login to post a comment.
