Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- int insertPrevElementsofHash(int curHash, unsigned char prevSymbol, unsigned char nextSymbol, const int* power, int len);
- void compareLines(const unsigned char* mask, const unsigned char* str, int curPos, int len1);
- int countHash(const unsigned char* str, int len, const int* power);
- int main() {
- unsigned char mask[17];
- int len1 = 0;
- for (int i = 0; i < 17; i++) {
- if (scanf("%c", &mask[i])) {}
- if (mask[i] == '\n') {
- mask[i] = '\0';
- len1 = i;
- break;
- }
- }
- int power[16];
- power[0] = 1;
- for (int j = 1; j < 16; j++)
- power[j] = power[j - 1] * 3;
- int hashOfMask = countHash(mask, len1, power);
- printf("%d ", hashOfMask);
- unsigned char str[17];
- for (int i = 0; i < len1; i++) {
- if (scanf("%c", &str[i]) != 1)
- return 0;
- }
- unsigned char buf[1001];
- int bufPos = 1000, curPos = 0, curHash = 0, inputResult = 0;
- curHash = countHash(str, len1, power);
- if (curHash == hashOfMask) compareLines (mask, str, curPos, len1);
- while (1) {
- if (bufPos == 1000) {
- inputResult = (int) fread(buf, sizeof(unsigned char), 1000, stdin);
- bufPos = 0;
- }
- if (inputResult == EOF || inputResult == bufPos) {
- return 0;
- }
- curHash = insertPrevElementsofHash(curHash, str[0], buf[bufPos], power, len1);
- char strnew[17];
- memcpy(strnew, str + 1, len1);
- memcpy(str, strnew, len1);
- str[len1 - 1] = buf[bufPos];
- bufPos++;
- curPos++;
- if (curHash == hashOfMask) compareLines(mask, str, curPos, len1);
- }
- }
- int insertPrevElementsofHash(int curHash, unsigned char prevSymbol, unsigned char nextSymbol, const int* power, int len) {
- return ((curHash - (prevSymbol % 3)) / 3) + (nextSymbol % 3) * power[len - 1];
- }
- void compareLines(const unsigned char* mask, const unsigned char* str, int curPos, int len1) {
- for (int i = 0; i < len1; i++) {
- printf("%d ", curPos + 1 + i);
- if (mask[i] != str[i]) return;
- }
- }
- int countHash(const unsigned char* str, int len, const int* power) {
- int hash = 0;
- for (int i = 0; i < len; i++) {
- hash = hash+(str[i] % 3) * power[i];
- }
- return hash;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement