Cod sursa(job #2067580)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 16 noiembrie 2017 17:05:26
Problema Potrivirea sirurilor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>
using namespace std;
 
char str[(int)4e6 + 10];
int z[sizeof(str)] = {} ;
 
void build_z(const int n){
    const int n = str.size();
    for(int i = 1, mij = 0, dr = 0; i < n; ++i){
        int j = (dr < i ? 0 : min(z[i-mij], dr-i+1));
        while(i+j < str.size() && str[i+j] == str[j]) ++j;
        z[i] = j;
        if(i+j-1 > dr) mij = i, dr = i+j-1; }
    return z; }

void build_z(const int n){
    for(int i = 1, mij = 0, dr = 0; i < n; ++i){
        int j = (dr < i ? 0 : min(z[i-mij], dr-i+1));
        while(str[i+j] == str[j]) ++j;
        z[i] = j;
        if(i+j-1 > dr) mij = i, dr = i+j-1; } }
 
int main(){
    ifstream f("strmatch.in");
    ofstream g("strmatch.out");
    f >> str;
    const int n = strlen(str);
    f >> (str+n+1);
    const int m = strlen(str+n+1);
    str[n] = '#';
 
    build_z(n+m+1);
 
    int rez = 0;
    vector<int> pozs;
    for(int i = 0; i < m; ++i){
        if(z[n+i+1] == n){
            ++rez;
            if(pozs.size() < 1000) pozs.push_back(i); } }
 
    g << rez << '\n';
    for(const auto x : pozs){
        g << x << ' '; }
 
    return 0; }