代码染色!
来源:互联网 发布:淘宝返利管理推荐关闭 编辑:程序博客网 时间:2024/04/20 16:44
// 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 17int 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;}
0 0
- 代码染色!
- 代码染色!
- 纯属利用代码染色功能
- BZOJ2243 [SDOI2011]染色 题解&代码
- 染色配对(详解+代码)
- 染色
- 染色
- 染色
- UE注释的代码染色问题
- lintcode python 代码 514 栅栏染色
- lintcode python 代码 514 栅栏染色
- 代码编辑器之关键字染色, 优化了的方案
- 代码编辑器之关键字染色, 优化了的方案
- 【bzoj 2243】【SDOI2011】染色 题解&代码(C++)
- POJ 1112 Team Them Up! (图(反图)染色+连通分量+动态规划) 解释代码
- [SDOI2011]染色
- 染色(paint)
- 选拔赛-染色
- ios 使用音频队列播放amr格式转换成wav的格式,只有嘈杂声
- SDK更新问题?求助!!!
- 15策略模式
- yii2数据库操作汇总
- SimpleAdapter重写时响应CheckBox复选框状态改变-源码修改
- 代码染色!
- 请教关于用asp自动发邮件的问题
- JSP内置对象之request对象使用案例
- Android 国际化混乱
- 文件系统命令
- 16状态模式
- Eclipse创建Web Maven项目
- Android的pull-to-refresh如何只使用上拉加载
- ndk编译无法识别so库