Cod sursa(job #1470788)

Utilizator s4d1ckOrtan Seby s4d1ck Data 12 august 2015 12:22:39
Problema Potrivirea sirurilor Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.85 kb
#include <stdio.h>
#include <vector>

#define MAX_SIZE 2000000
#define MAX_MATCHES 1000
#define IN_FILE  "strmatch.in"
#define OUT_FILE "strmatch.out"

using namespace std;

typedef struct {
    long first_pos;
    long next_match;
} potential_t;

int main(){
    FILE *in_file, *out_file;
    char c;
    char pos[MAX_SIZE];
    long i = 0;
    char letter1;
    vector<potential_t> potentials; 
    vector<potential_t>::iterator it;
    long sol[MAX_MATCHES];
    int total_matches = 0;
    in_file = fopen(IN_FILE, "r");
    out_file = fopen(OUT_FILE, "w");
    fscanf(in_file, "%c", &letter1);
    
    while (1 == 1){
        fscanf(in_file, "%c", &c);
        pos[i] = c;
        i++;
        if (c == '\n') break;
    }
        
    i = -1;
    while (1 == 1){
        fscanf(in_file, "%c", &c);
        i++;
        if (c == letter1){
            potential_t new_pot;
            new_pot.first_pos = i;
            new_pot.next_match = 0;
            potentials.push_back(new_pot);
        }
        it = potentials.begin();
        while (it != potentials.end()){
            if (it->first_pos == i){
                it++;
                continue;
            }
            if (pos[it->next_match] == '\n'){
                sol[total_matches] = it->first_pos;
                if (total_matches > MAX_MATCHES) break;
                total_matches++;
                potentials.erase(it);
                continue;
            } 
            if (pos[it->next_match] != c){
                potentials.erase(it);
                continue;
            }
            it->next_match++;
            it++;
        }
        if (total_matches > MAX_MATCHES || c == '\n') break;
    }
    fclose(in_file);
    
    fprintf(out_file, "%d\n", total_matches);
    for (i = 0; i < total_matches; i++){
        fprintf(out_file, "%lu ", sol[i]);
    }
    
    fclose(out_file);
        
    return 0; 
}