Cod sursa(job #2200345)

Utilizator icansmileSmileSmile icansmile Data 1 mai 2018 00:54:29
Problema Potrivirea sirurilor Scor 14
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 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;
    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) {
            fprintf(output, "%lu ", position);
            index++;
        }

        numberOfElements++;

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

    fprintf(output, "%d\n", numberOfElements);

    fclose(input);
    fclose(output);

    return 0;
}