Cod sursa(job #1338384)

Utilizator sifushifMihaela Muraru sifushif Data 9 februarie 2015 23:24:28
Problema Potrivirea sirurilor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include<iostream>
#include<string.h>
#include<fstream>
#include<vector>
using namespace std;
	
ifstream f("strmatch.in");
ofstream g("strmatch.out");

char str1[2000010], str2[2000010];
int n , m;
int p[2000010];
vector<int> w;


void KMP_prefix()
{
	p[1] = 0;
	int k = 0;
	for(int i=2; i<=n; i++)
	{
		while(k>0 && str1[k+1] != str1[i]) k = p[k];
		if(str1[k+1]==str1[i]) k=k+1;
		p[i] = k;
	}
}

void KMP_potrivire()
{

	int q = 0;	
	for(int i=0; i<=m; i++)
	{
		while(q>0 && str1[q+1] != str2[i]) 
			q = p[q];
		
		if(str1[q+1]==str1[i])
			q=q+1;
		
		if(q==n)
			w.push_back (i-n);
	}	
}

int main()
{
	int n = strlen(str1);
	int m = strlen(str2);	

	KMP_prefix();
	KMP_potrivire();
	for(int i = 1;i <= w.size() ;i++  ){
		g<<w[i]<<" ";
	}

return 0;
}