Cod sursa(job #2572833)

Utilizator RazvanPanaiteRazvan Panaite RazvanPanaite Data 5 martie 2020 14:28:58
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda OJI 2020 Marime 1.42 kb
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

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

void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

typedef pair<int,int> pii;
typedef long long int ll;
typedef long double ld;

const int DMAX = 2e6+10;

vector <int> sol;

char a[DMAX];
char b[DMAX];

int dp[DMAX];

int ans;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t,i,j;
    int lga,lgb,k;

    fin>>(a+1);
    fin>>(b+1);

    lga=strlen(a+1);
    lgb=strlen(b+1);

    k=0;
    for(i=2;i<=lga;i++){
        while(a[k+1] != a[i] && k)
            k=dp[k];
        if(a[k+1] == a[i])
            k++;
        dp[i]=k;
    }

    k=0;
    if(a[1] == b[1])
        k=1;
    for(i=2;i<=lgb;i++){
        while(a[k+1] != b[i] && k)
            k=dp[k];
        if(a[k+1] == b[i])
            k++;
        if(k == lga){
            ans++;
            if(sol.size() < 1000)
                sol.pb(i-lga);
        }
    }
    fout<<ans<<'\n';
    for(auto& it:sol)
        fout<<it<<' ';
    fout<<'\n';

    return 0;
}