// KMP 算法
#include<cstdio>
#include <windows.h>
int n, m, pos, pi[102];
char p[102], t[150002];
char file[30] = "..\\output\\output_ .txt";
int Compute_Prefix_Function( );
int KMP_Match( );
int main( )
{
freopen( "..\\input\\input.txt", "r", stdin );
LARGE_INTEGER nFreq;
LARGE_INTEGER nBeginTime;
LARGE_INTEGER nEndTime;
double time1, time2;
for( int num = 1; num <= 5; num++ )
{
scanf( "%d%d", &n, &m );
scanf( "%s%s", t+1, p+1 );
file[17] = 48 + num;
freopen( file, "a", stdout );
QueryPerformanceFrequency( &nFreq );
QueryPerformanceCounter( &nBeginTime );
Compute_Prefix_Function( );
QueryPerformanceCounter(&nEndTime);
time1=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart * 1000000;
QueryPerformanceCounter( &nBeginTime );
pos = KMP_Match( );
QueryPerformanceCounter(&nEndTime);
time2=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart * 1000000;
printf( "KMP:\n" );
printf( " first match position: %d\n", pos );
printf( " time for pretreatment: %.3lf\n", time1 );
printf( " time for first match: %.3lf\n", time2 );
printf( "\n" );
fclose( stdout );
}
}
int Compute_Prefix_Function( )
{
pi[1] = 0;
int k = 0;
for( int q = 2; q <= m; q++ )
{
while( k > 0 && p[k+1] != p[q] ) k = pi[k];
if( p[k+1] == p[q] ) k++;
pi[q] = k;
}
}
int KMP_Match( )
{
int q = 0;
for( int i = 1; i <= n; i++ )
{
while( q > 0 && p[q+1] != t[i] ) q = pi[q];
if( p[q+1] == t[i] ) q = q + 1;
if( q == m )
return( i - m + 1 );
}
return -1;
}
// Rabin Karp 算法
#include<cstdio>
#include <windows.h>
#define prime 11117
#define d 17
int n, m, h, pos, phash, thash;
char p[102], t[150002];
char file[30] = "..\\output\\output_ .txt";
int Pretreatment( );
int Rabin_Karp_Match( );
int main( )
{
freopen( "..\\input\\input.txt", "r", stdin );
LARGE_INTEGER nFreq;
LARGE_INTEGER nBeginTime;
LARGE_INTEGER nEndTime;
double time1, time2;
for( int num = 1; num <= 5; num++ )
{
scanf( "%d%d", &n, &m );
scanf( "%s%s", t+1, p+1 );
file[17] = 48 + num;
freopen( file, "a", stdout );
QueryPerformanceFrequency( &nFreq );
QueryPerformanceCounter( &nBeginTime );
Pretreatment( );
QueryPerformanceCounter(&nEndTime);
time1=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart * 1000000;
QueryPerformanceCounter( &nBeginTime );
pos = Rabin_Karp_Match( );
QueryPerformanceCounter(&nEndTime);
time2=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart * 1000000;
printf( "Rabin Karp:\n" );
printf( " first match position: %d\n", pos );
printf( " time for pretreatment: %.3lf\n", time1 );
printf( " time for first match: %.3lf\n", time2 );
printf( "\n" );
fclose( stdout );
}
}
int Pretreatment( )
{
int c;
h = 1;
for( int i = m; i; i-- )
{
phash = ( phash + h * p[i] ) % prime;
thash = ( thash + h * t[i] ) % prime;
h = ( h * d ) % prime;
}
}
int Rabin_Karp_Match( )
{
int c;
for( int i = 1; i <= n-m+1; i++ )
{
if( phash == thash )
{
int j = 1;
while( j <= m && p[j] == t[i+j-1] ) j++;
if( j == m+1 ) return i;
}
if( i <= n-m )
thash = ( d*( thash - h * t[i] ) + t[i+m] + prime ) % prime;
}
return -1;
}
// Quick_Search 算法
#include<cstdio>
#include <windows.h>
int n, m, pos, Bc[302];
char p[102], t[150002];
char file[30] = "..\\output\\output_ .txt";
int Compute_Pre_Bc( );
int Quick_Search( );
int main( )
{
freopen( "..\\input\\input.txt", "r", stdin );
LARGE_INTEGER nFreq;
LARGE_INTEGER nBeginTime;
LARGE_INTEGER nEndTime;
double time1, time2;
for( int num = 1; num <= 5; num++ )
{
scanf( "%d%d", &n, &m );
scanf( "%s%s", t+1, p+1 );
file[17] = 48 + num;
freopen( file, "a", stdout );
QueryPerformanceFrequency( &nFreq );
QueryPerformanceCounter( &nBeginTime );
Compute_Pre_Bc( );
QueryPerformanceCounter(&nEndTime);
time1=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart * 1000000;
QueryPerformanceCounter( &nBeginTime );
pos = Quick_Search( );
QueryPerformanceCounter(&nEndTime);
time2=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart * 1000000;
printf( "Quick_Search:\n" );
printf( " first match position: %d\n", pos );
printf( " time for pretreatment: %.3lf\n", time1 );
printf( " time for first match: %.3lf\n", time2 );
printf( "\n" );
fclose( stdout );
}
}
int Compute_Pre_Bc( )
{
for( int i = 0; i <= 255; i++ )
Bc[i] = m + 1;
for( int i = 1; i <= m; i++ )
Bc[ p[i] ] = m + 1 - i;
}
int Quick_Search( )
{
int i = 1;
while( i <= n-m+1 )
{
int j = 1;
while( j <= m && p[j] == t[i+j-1] ) j++;
if( j == m+1 ) return i;
i = i + Bc[ t[i+m] ];
}
return -1;
}