Cod sursa(job #2555433)

Utilizator astroman389Claudiu Negru astroman389 Data 24 februarie 2020 00:47:27
Problema Potrivirea sirurilor Scor 80
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *fin, *fout;
void find(char *tofind, char *s)
{
    long int nr = 0, nrp = 0, positions[1000000];
    char *p = strstr(s, tofind);

    if(p == NULL)
    {
        fprintf(fout,"%ld",nr);
        return;
    }

    nr++;
    positions[nrp++] = p - s;
    while((p = strstr(p+1, tofind)) != NULL)
    {
        nr++;
        positions[nrp++] = p - s;
    }

    fprintf(fout,"%ld\n", nr);
    for(int i = 0; i < nrp; i++)
    {
        fprintf(fout, "%ld ",positions[i]);
        if(i == 999)return;
    }

}

int main()
{
    fin = fopen("strmatch.in","r"), fout = fopen("strmatch.out","w");
    char s1[2000000], s2[2000000];
    /*
    fgets(s1, 2000000, fin);
    int l1 = strlen(s1);
    fgets(s2, 2000000, fin);
    s1[l1-1] = '\0';
    */
  //  putchar('A');
    int i = 0, j = 0;
    char c;
    while((c = fgetc(fin)) != '\n')
    {
        s1[i++] = c;
    }
    s1[i] = '\0';
    while((c = fgetc(fin)) != EOF && c != '\n')
    {
        s2[j++] = c;
    }
    s2[j] = '\0';
    find(s1,s2);

    fclose(fin);fclose(fout);
    return 0;
}