Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int countHash(const unsigned char* str, int len, const int* power) {
  4.     int hash = 0;
  5.     for (int i = 0; i < len; i++) {
  6.         hash = hash + (str[i] % 3) * power[i];
  7.     }
  8.     return hash;
  9. }
  10.  
  11. int CountNewHash(int curHash, unsigned char prevSymbol, unsigned char nextSymbol, const int* power, int len) {
  12.     return ((curHash - (prevSymbol % 3)) / 3) + (nextSymbol % 3) * power[len - 1];
  13. }
  14.  
  15. void compareLines(const unsigned char* mask, const unsigned char* str, int curPos, int lenMask) {
  16.     for (int i = 0; i < lenMask; i++) {
  17.         printf("%d ", curPos + 1 + i);
  18.         if (mask[i] != str[i]) return;
  19.     }
  20. }
  21.  
  22. int main() {
  23.     unsigned char mask[17];
  24.     int lenMask = 0;
  25.     for (int i = 0; i < 17; i++) {
  26.         if (scanf("%c", &mask[i])) {}
  27.         if (mask[i] == '\n') {
  28.             mask[i] = '\0';
  29.             lenMask = i;
  30.             break;
  31.         }
  32.     }
  33.     int power[16];
  34.     power[0] = 1;
  35.     for (int j = 1; j < 16; ++j)
  36.         power[j] = power[j-1] * 3;
  37.     int hashOfMask = countHash(mask, lenMask, power);
  38.     printf("%d ", hashOfMask);
  39.     unsigned char str[17];
  40.     for (int i = 0; i < lenMask; i++) {
  41.         if (scanf("%c", &str[i]) != 1) return 0;
  42.     }
  43.     unsigned char buffer[1000];
  44.     int bufferPos = 1000, curPos = 0, curHash = 0, inputResult = 0; //curPos - текущая позиция в тектсе;
  45.     curHash = countHash(str, lenMask, power);
  46.     if (curHash == hashOfMask) compareLines(mask, str, curPos, lenMask);
  47.     while (1) {
  48.         if (bufferPos == 1000) {
  49.             inputResult = (int) fread(buffer, sizeof(unsigned char), 1000, stdin);
  50.             bufferPos = 0;
  51.         }
  52.         if (inputResult == EOF || inputResult == bufferPos) {
  53.             return 0;
  54.         }
  55.         curHash = CountNewHash(curHash, str[0], buffer[bufferPos], power, lenMask);
  56.         for (int i = 0; i < lenMask - 1; i++) {
  57.             str[i] = str[i+1];
  58.         }
  59.         str[lenMask - 1] = buffer[bufferPos];
  60.         ++bufferPos;
  61.         ++curPos;
  62.         if (curHash == hashOfMask) compareLines(mask, str, curPos, lenMask);
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement