Cod sursa(job #1474140)

Utilizator s4d1ckOrtan Seby s4d1ck Data 21 august 2015 00:52:19
Problema Potrivirea sirurilor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.26 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, it;
    char letter1;
    long potential_size = 0;
    potential_t potentials2[MAX_SIZE];
    
    //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);
            //potentials[potential_size] = new_pot;
            potentials2[potential_size] = new_pot;
            potential_size++;
        }
        it = 0;
        while (it <= potential_size){
            if (potentials2[it].first_pos == i){
                it++;
                continue;
            }
            if (pos[potentials2[it].next_match] == '\n'){
                if (total_matches < MAX_MATCHES)
                {
                    sol[total_matches] = potentials2[it].first_pos;
                }
                total_matches++;
                //potentials.erase(it);
                potential_size--;
                continue;
            } 
            if (pos[potentials2[it].next_match] != c){
                //potentials.erase(it);
                potential_size--;
                continue;
            }
            potentials2[potential_size].next_match++;
            //it->next_match++;
            it++;
        }
        if (c == '\n') break;
    }
    fclose(in_file);
    
    fprintf(out_file, "%d\n", total_matches);
    for (i = 0; i < total_matches; i++){
        if (i == MAX_MATCHES) break;
        fprintf(out_file, "%lu ", sol[i]);
    }
    
    fclose(out_file);
        
    return 0; 
}