Posted By

merkator on 09/28/10


Tagged

hash work cmp doesnt


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

merkator


CMP


 / Published in: C++
 

This code doesn't work... Every time it sends -1 in answer.

  1. #include<cstdio>
  2. #include<cstring>
  3.  
  4. using namespace std;
  5.  
  6. #define P (int)1e9+7
  7.  
  8. long long hash(char *str, int x, int y){
  9. long long tmp=0;
  10. for(int i=y; i>=x; i--){
  11. tmp*=P;
  12. tmp+=str[i];
  13. }
  14. return tmp;
  15. }
  16.  
  17. int main(){
  18. char str[1000];
  19. char substr[20];
  20. scanf("%s",str);
  21. scanf("%s",substr);
  22. int z=strlen(substr);
  23. long long hash1=hash(substr,0,z);
  24. printf("%lld\n",hash1);
  25. int len=strlen(str)-z;
  26. long long sum[len];
  27. sum[0]=hash(str,0,z);
  28. printf("%lld\n",sum[0]);
  29. for(int i=1; i<len; i++){
  30. sum[i]=sum[i-1]*P+str[i+z];
  31. printf("%lld\n",sum[i]);
  32. }
  33. for(int i=0; i<len; i++){
  34. if(sum[i]==hash1){
  35. printf("%d\n",i);
  36. return 0;
  37. }
  38. }
  39. printf("-1\n");
  40. return 0;
  41. }

Report this snippet  

You need to login to post a comment.