Cod sursa(job #2200344)

Utilizator icansmileSmileSmile icansmile Data 1 mai 2018 00:50:55
Problema Potrivirea sirurilor Scor 80
Compilator c Status done
Runda Arhiva educationala Marime 1.15 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LENGTH 2000000

int min(int first, int second) {
    if (first < second)
        return first;
    return second;
}

int main() {
    char *first, *second;
    unsigned long positions[1000];
    int numberOfElements = 0;
    int index = 0;
    unsigned long position;

    FILE *input = fopen("strmatch.in", "r");
    FILE *output = fopen("strmatch.out", "w");

    first = (char *)malloc(MAX_LENGTH * sizeof(char));
    second = (char *)malloc(MAX_LENGTH * sizeof(char));

    fscanf(input, "%s", first);
    fscanf(input, "%s", second);

    char *p = strstr(second, first);

    while (p != NULL) {
        position = p - second;

        if (index < 1000) {
            positions[index] = position;
            index++;
        }

        numberOfElements++;

        p = strstr(p + 1, first);
    }

    fprintf(output, "%d\n", numberOfElements);
    int minValue = min(1000, numberOfElements);

    for (int i = 0; i < minValue; i++) {
        fprintf(output, "%lu ", positions[i]);
    }

    fclose(input);
    fclose(output);

    return 0;
}