




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、3¡¢´®Æ¥ÅäËã·¨ÓÃÓïÑÔʵÏÖÂùÁ¦·¨¡¢Horspool¡¢Boyer-Moore¡¢Knuth-Morris-PrattËã·¨£¬Õë¶
2、2;²»Í¬µÄÊý¾Ý¹æÄ£Ñо¿ËüÃǵÄÐÔÄÜ¡£Êý¾Ý¹æÄ£Ó¦ÔÚ100£¬000ÒÔÉÏ
3、1;£int BruteForceStringMatch(string str, string pattern)for( int i = 0; i <= str.size()- pattern.size(); i+ )int j = 0;while( (j < pattern.size() && (patternj = stri+j)j+;if( j = pattern.size()return i;return (-1);void ShiftTable(string pattern, int table, int l)for(int i = 0; i &
4、lt; 127; i+)tablei = l;for(int j = 0; j < l-1; j+)tablepatternj = l-1-j;void HorspoolMatch(string str, int table, string pSize)int main()string a = "abcdefg"string mode = "z"cout<<BruteForceStringMatch(a, mode)<<endl;return 0;#include<iostream>using namespace
5、 std;const int HASH_SIZE=256;int tableHASH_SIZE;/¶ÔÓ¦×Ö·û±íµÄ255¸ö×Ö·û½¨¹þÏ£±í,±íʾ¶Ô²»Æ¥Åä×Ö·&
6、#251; ÏòÓÒÒÆ¶¯µÄ¾àÀëvoid ShiftTable(char pattern)/*½¨Á¢Ò»¸öÒÔ×Öĸ±íÖÐ×Ö·ûΪË÷ÒýµÄ
7、;TableÊý×é*/int m=strlen(pattern);for(int i=0;i<HASH_SIZE;i+) tablei=m;for(int j=0;j<m-1;j+) table patternj =m-1-j;int HorspoolMatching(char pattern,char text)/ƽ¾ùЧÂÊΪO(n)/*pre:ģʽpattern£¬&
8、#206;ı¾textpost:µÚÒ»¸öÆ¥Åä×Ö´®µÄ×î×ó¶Ë×Ö·ûϱ꣬ûÓÐÕÒµ½Æ¥Åä
9、5;Ö´®·µ»Ø-1*/ShiftTable(pattern);int m=strlen(pattern);int n=strlen(text);int i=m-1;while(i <= n-1) int k=0; /Æ¥ÅäµÄ×Ö·û¸öÊý while(k<=m-1 && patternm-1-k = texti-k ) k+; if(k=m) ret
10、urn i-m+1; else i=i+table texti ; /ÒÔģʽ×îºóÒ»¸ö×Ö·ûÈ·¶¨Òƶ¯¾àÀë,ÓëB.MËã·¨µÄ×î´óÇ
11、48;±ð£¬¼ò»¯ÐÎʽreturn -1;int main()char p20,t1000;while(cin>>t && t!=".") int times=5; while(times-) cin>>p; cout<<HorspoolMatching(p,t)<<endl; return 1; #include <iostream> #include <algor
12、ithm> #include <string> #include <vector> #ifndef ssize_t typedef off_t ssize_t; #endif using namespace std; void compute_last_occurrence(const string& needle , vector<ssize_t>& last_occurrence) last_occurrence.resize(256,-1); for ( size_t i = 0 ; i < needle.size() ;
13、i+ ) last_occurrenceneedlei = i; void compute_prefix_function(const string& needle , vector<size_t>& prefix_function) if ( needle.size() = 0 ) return; prefix_function.resize( needle.size() , 0 ); size_t d = 0 ; for ( size_t i = 1 ; i < needle.size() ; i+ ) while ( d > 0 &&
14、; needled != needlei ) d = prefix_functiond-1; if ( needlei = needled ) d+; prefix_functioni=d; void compute_goodsuffix_heuristic( const string& needle ,vector<size_t>& goodsuffix_heristic) string reverse_needle; copy( needle.rbegin() , needle.rend() , back_inserter( reverse_needle ) )
15、; vector<size_t> prefix_function,reverse_prefix_function; compute_prefix_function( needle , prefix_function ); compute_prefix_function( reverse_needle , reverse_prefix_function ); size_t m = needle.size(); goodsuffix_heristic.resize( m + 1 , m - prefix_function m - 1 ); for ( size_t l = 1 ; l
16、<= m ; l+ ) size_t t = reverse_prefix_functionl-1; size_t j = m - t ; if ( goodsuffix_heristicj > l - t ) goodsuffix_heristicj = l - t ; void boyer_moore_matcher(const string& haystack,const string& needle) size_t n=haystack.size(); size_t m=needle.size(); if ( n = 0 | m = 0 | n < m
17、 ) cout<<"Invalid input"<<endl; return; vector<ssize_t> last_occurrence; compute_last_occurrence(needle,last_occurrence); vector<size_t> goodsuffix_heristic; compute_goodsuffix_heuristic( needle , goodsuffix_heristic ); size_t offset = 0 ; while ( offset <= n - m
18、 ) ssize_t j = m - 1 ; while ( j >= 0 && needlej = haystackoffset+j ) j-; if ( j < 0 ) cout<<"Find a match at offset "<<offset<<endl; offset += goodsuffix_heristic0; else offset += max<size_t>( goodsuffix_heristicj+1 , j - last_occurrencehaystackoffset
19、+j ); int main() string haystack,needle; while ( true ) cout<<"Input the haystack:"<<endl; getline(cin,haystack); cout<<"Input the needle:"<<endl; getline(cin,needle); cout<<"Match results:"<<endl; boyer_moore_matcher(haystack,needle)
20、; #include <iostream> #include <algorithm> #include <string> #include <iostream> using namespace std; int* GetNextVal(const char *s, int &len) len = strlen(s); int *next = new intlen; int i = 0; int j = -1; next0 = -1; while(i<len-1)/×¢ÒâÕâÀï¸úKMPº¯ÊýÀïÃæµÄ²»Í¬ if(j=-1 | si=sj) +i; +j; nexti = j; else j = nextj; return next; int KMP(const char *s, const char *t) int slen,tlen;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 基于情境教學的初中化學課堂教學效果評價研究論文
- 小學語文實驗:磁鐵磁力衰減故事創作的語文實驗探究與反思方法與技巧論文
- 基于綠色物流的電商包裝廢棄物回收體系優化論文
- 小學語文革命題材課文教學中的問題意識與解決策略研究論文
- 中國醫藥中間體市場供需格局及投資風險分析報告2025年
- 節假日公司管理制度
- 苗木進出庫管理制度
- 茶藝師外派管理制度
- 溶液-2021中考化學一模分類匯編(上海專用)
- 財務管理職位操作指南
- 2023年中國化學奧林匹克競賽浙江省預賽試題及參考答案
- RB/T 089-2022綠色供應鏈管理體系要求及使用指南
- 優秀傳統文化在高中政治教學中的應用策略 論文
- 匯川MD系列變頻器說明書文檔全文預覽
- 柴油機外文文獻翻譯資料
- GB/T 10963.1-2020電氣附件家用及類似場所用過電流保護斷路器第1部分:用于交流的斷路器
- GA/T 1567-2019城市道路交通隔離欄設置指南
- RASS鎮靜評分表格
- YY 0331-2006 脫脂棉紗布、脫脂棉粘膠混紡紗布的性能要求和試驗方法
- 切分軋制孔型設計
- 轉化國際食品法典(CAC)農藥最大殘留限量標準
評論
0/150
提交評論