Cod sursa(job #2611203)

Utilizator EdythestarGhiriti Edmond Edythestar Data 6 mai 2020 16:05:45
Problema Potrivirea sirurilor Scor 14
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <iostream>
#include<fstream>
#include<string>
#include <vector>

using namespace std;

int main() {
	string minta, str;
	ifstream fin("strmatch.in");
	ofstream fout("strmatch.out");
	getline(fin, minta);
	getline(fin, str);
	int minta_hossz = minta.size();
	int str_hossz = str.size();
    int db = 0;
    vector<int> a;
  /*  for (int i = 0; i < str_hossz; i++) {
        int j;
        for (j = 0; j < minta_hossz; j++) {
            if (str[i + j] != minta[j]) break;
            if (j == minta_hossz - 1) {
                a.push_back(i);
                db++;
            }
        }
    } */
    int* prefix;
    prefix = new int[minta_hossz + 1];
    prefix[0] = 0;
    prefix[1] = 0;
    int k = 0;
    for (int i = 1; i < minta_hossz; i++) {
        while (k > 0 && minta[k] != minta[i])
            k = prefix[k];

        if (minta[k] == minta[i])
            k++;

        prefix[i + 1] = k;
    }

    int i = 0;
    int j = 0;
   
    while (i < str_hossz) {
   
        if (minta[j] == str[i]) {
            if (j == minta_hossz - 1) {
                a.push_back(i - j);
                db++;
            }
            i++;
            j++;
        }
        else {

            i++;
            j = prefix[j] + 1;
        }
    }

    fout << db << "\n";
    if (db > 1000) {
        for (int i = 0; i <= 999; i++) {
            fout << a[i] << " ";
        }
    }
    else {
        for (auto& e : a) {
            fout << e << " ";
        }
    }
}