Cod sursa(job #2491565)

Utilizator IATI2019Iati Shumen IATI2019 Data 12 noiembrie 2019 19:22:12
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.66 kb
#include <bits/stdc++.h>

using namespace std;
int phi[4000002];
vector <int> sol;
class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};
int main()
{

    ifstream cin("strmatch.in");
    ofstream cout("strmatch.out");
    string a,b;  ios_base::sync_with_stdio(false);
    cin.tie(0);
    int init;
    int cnt = 0;
    cin >> a >> b;
    init = a.size();
    a += "#";
    a += b;
    int n = a.size();
    for(int i = 1;i < n;i++){
        int j = phi[i - 1];
        while(a[j] != a[i] && j > 0){
            j = phi[j - 1];
        }
        if(a[j] == a[i])
            j++;
        phi[i] = j;
        if(phi[i] == init)
        sol.push_back(i - 2 * init);
    }
    cout << sol.size() << "\n";
    for(int i = 0;i < min(1000,(int)sol.size());i++)
        cout << sol[i] << " ";
    return 0;
}