Advertisement
Guest User

Untitled

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