Cod sursa(job #2719731)

Utilizator halexandru11Hritcan Alexandru halexandru11 Data 10 martie 2021 11:11:29
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.73 kb
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>
using namespace std;

/// START Errichto's debug
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; }
/// END Errichto's debug

#define ff first
#define ss second
#define eb emplace_back
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define f0(i, n) for(auto i = 0; i < n; ++i)
#define fo(i, k, n) for(auto i = k; i < n; ++i)
#define foo(i, a, b, k) for(auto i = a; i < b; i += k)
#define rf0(n, i) for(auto i = n-1; ~i; --i)
#define rfo(n, k, i) for(auto i = n-1; i >= k; --i)
#define rfoo(b, a, k, i) for(auto i = b; i > a; i -= k)
#define Im_speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)

typedef long double ld;
typedef uint32_t uint;
typedef int64_t ll;
typedef uint64_t ull;

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

const int nax = 1e5 + 3;
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const ll infll = 1e18 + 7;
const ld PI = 3.1415926535897932384626433832795;

void test_case() {
    string pattern, s;
    fin >> pattern >> s;

    int np = (int) pattern.size();
    int ns = (int) s.size();
    vector<int> delta(np+1, 0);

    int index = 1, len = 0;
    while(index < np) {
        if(pattern[index] == pattern[len]) {
            delta[index++] = ++len;
        }
        else {
            if(len) {
                len = delta[len-1];
            }
            else {
                ++index;
            }
        }
    }

    vector<int> ans;
    index = 0, len = 0;
    while(index < ns) {
        if(s[index] == pattern[len]) {
            ++index, ++len;
        }
        else {
            if(len) {
                len = delta[len-1];
            }
            else {
                ++index;
            }
        }
        if(len == np) {
            ans.pb(index-len);
            len = delta[len-1];
        }
//        if((int) ans.size() == 1000) {
//            break;
//        }
    }

    debug() << imie(delta);
    fout << ans.size() << "\n";
    for(int it : ans) {
        fout << it << " ";
    }
}

int main() {
//    clock_t _clock = clock();
    Im_speed;
    cout << fixed << setprecision(12);

    int t = 1;
//    cin >> t;
    while(t--) {
        test_case();
    }

//    cout << "\n\nTime: " << ((((double) clock()) - ((double)_clock)) / ((double) CLK_TCK));
}