Cod sursa(job #2677105)

Utilizator walentines44Serban Valentin walentines44 Data 25 noiembrie 2020 20:07:03
Problema Potrivirea sirurilor Scor 24
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.49 kb
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

char string_array[2000005], substring_array[2000005];

ifstream fin("strmatch.in");
ofstream fout("strmatch.out");

void convert_to_char(char sir_array[], string sir){
    strcpy(sir_array, sir.c_str());
}

void citire_date(char sir_array[], char subsir_array[]){
    string subsir, sir;
    getline(fin, subsir);
    getline(fin, sir);
    convert_to_char(sir_array, sir);
    convert_to_char(subsir_array, subsir);
}


void strmatch(char sir[], char subsir[]){
    int n_1 = strlen(sir);
    int vec[10000], cnt = 0;
    char *value;
    int new_value = 0, OK = 1;
    for(int i = 0; i < n_1; i++){
        for(int j = new_value; j < n_1; j++){
            value = strstr(sir + new_value + 1, subsir);
            if(value){
                new_value = value - sir;
                if(cnt <= 1000){
                    vec[cnt] = new_value;
                    OK = 0;
                }
                else{
                    OK = 1;
                }
                cnt++;
                new_value += 1;
                break;
            }
        }
    }
    fout << cnt << "\n";
    cout << cnt;
    if(OK == 0){
        for(int i = 0; i < cnt; i++){
            fout << vec[i] << " ";
        }
    }
    else{
        for(int i = 0; i < 1000; i++){
            fout << vec[i] << " ";
        }
    }
}

int main() {

    citire_date(string_array, substring_array);
    strmatch(string_array, substring_array);

    return 0;
}