Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int calculateHash(const unsigned char* str, int len, const int* power) {
  5. int hash = 0;
  6. for (int i = 0; i < len; i++) {
  7. hash = hash+(str[i] % 3) * power[i];
  8. }
  9. return hash;
  10. }
  11.  
  12. int getHashFromHash(int curHash, unsigned char prevSymbol, unsigned char nextSymbol, const int* power, int len) {
  13. return ((curHash - (prevSymbol % 3)) / 3) + (nextSymbol % 3) * power[len - 1];
  14. }
  15.  
  16. void compareLines(const unsigned char* mask, const unsigned char* str, int curPos, int len1) {
  17. for (int i = 0; i < len1; i++) {
  18. printf("%d ", curPos + 1 + i);
  19. if (mask[i] != str[i]) return;
  20. }
  21.  
  22. }
  23.  
  24. int main() {
  25. unsigned char mask[17];
  26. int len1 = 0;
  27. for (int i = 0; i < 17; i++) {
  28. if (scanf("%c", &mask[i])) {}
  29. if (mask[i] == '\n') {
  30. mask[i] = '\0';
  31. len1 = i;
  32. break;
  33. }
  34. }
  35.  
  36. int power[16];
  37. power[0] = 1;
  38. for (int j = 1; j < 16; j++)
  39. power[j] = power[j - 1] * 3;
  40.  
  41. int hashOfMask = calculateHash(mask, len1, power);
  42. printf("%d ", hashOfMask);
  43.  
  44. unsigned char str[17];
  45. for (int i = 0; i < len1; i++) {
  46. if (scanf("%c", &str[i]) != 1)
  47. return 0;
  48. }
  49.  
  50. unsigned char buf[1001];
  51.  
  52. int bufPos = 1000, curPos = 0, curHash = 0, inputResult = 0;
  53.  
  54. curHash = calculateHash(str, len1, power);
  55. if (curHash == hashOfMask) compareLines(mask, str, curPos, len1);
  56.  
  57. while (1) {
  58. if (bufPos == 1000) {
  59. inputResult = (int) fread(buf, sizeof(unsigned char), 1000, stdin);
  60. bufPos = 0;
  61. }
  62.  
  63. if (inputResult == EOF || inputResult == bufPos) {
  64. return 0;
  65. }
  66.  
  67. curHash = getHashFromHash(curHash, str[0], buf[bufPos], power, len1);
  68. char strnew[17];
  69. memcpy(strnew, str + 1, len1);
  70. memcpy(str, strnew, len1);
  71. str[len1 - 1] = buf[bufPos];
  72.  
  73. bufPos++;
  74. curPos++;
  75.  
  76. if (curHash == hashOfMask) compareLines(mask, str, curPos, len1);
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement