Cod sursa(job #931356)

Utilizator palcuiealexAlex Palcuie palcuiealex Data 28 martie 2013 10:28:17
Problema Potrivirea sirurilor Scor 38
Compilator cpp Status done
Runda Arhiva educationala Marime 2.31 kb
/***************************************************
 * Alex Palcuie
 * Romania - 2013
 * alex [dot] palcuie [at] gmail [dot] com
 * http://palcu.blogspot.com/
****************************************************/

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <utility>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iterator>
#include <cstring>
#include <climits>
#include <fstream>
#include <sstream>

using namespace std;

// Defines
#define NAME(n) #n
#define prv(v,n) dbv((v), (#v), (n))
#define prw(v) dbw((v), (#v))
#define F first
#define S second
#define pb push_back
#define sz size()
#define mp make_pair
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

// Helpers
template <typename T> inline void dbv(const T * v, const char * name, int n){
	fprintf(stderr, "=== %s ===\n", name);
	for(int i=0; i<n; i++)
		cerr << v[i] << " ";
	cerr << '\n';
}
template<typename T> void dbw(const std::vector<T>& t, const char * name){
	fprintf(stderr, "=== %s ===\n", name);
	unsigned n = t.size();
	for(typename std::vector<T>::size_type i=0; i<n; ++i)
		std::cerr << t[i] << ' ';
	cerr << '\n';
}

// Structs

// Constants
const int N = 2000008;
const int INF = 0x3f3f3f3f;

// Globals
string s, p;
int a[N];
vector<int> sol;

// Functions
int main(){
	ios_base::sync_with_stdio(false);
	#ifndef ONLINE_JUDGE
	ifstream fin("strmatch.in");
	ofstream fout("strmatch.out");
	#endif

	getline(fin, s);
	getline(fin, p);

	int i, j;
	int right = 1, left = 0;
	while(right < s.sz){
        while(s[left] != s[right] && right < s.sz)
            right++;
        while(s[left] == s[right] && right < s.sz){
            a[right] = a[right-1] + 1;
            right++; left++;
        }
	}

    int nSol = 0;
    for(i=0; i<p.length();i++){
        int k = i, j = 0;
        while(s[j] && p[k] && s[j] == p[k]){
            j++; k++;
        }
        if(j == s.length()){
            nSol++;
            if(sol.sz < 1000)
                sol.pb(i);
        }
        if(a[j-1])
            i += j - a[j-1] - 2;
    }

    fout << nSol << '\n';
    for(i=0; i<sol.sz; i++)
        fout << sol[i] << ' ';
    fout << '\n';

	fin.close(); fout.close(); return 0;
}