




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國熱熔斷器行業(yè)市場深度研究及發(fā)展趨勢預(yù)測報告
- 玻璃鋼通風機項目投資可行性研究分析報告(2024-2030版)
- 教師情緒管理指南
- 2025年 雅安市市級機關(guān)遴選考試筆試試題附答案
- 中國石油化工用加氫反應(yīng)器市場前景預(yù)測及投資規(guī)劃研究報告
- 儀器儀表項目可行性報告
- 2025年 丹東鳳城市公立醫(yī)院普通高校招聘考試筆試試題附答案
- 2025年 保健按摩師高級職業(yè)技能考試試題附答案
- 中國擴孔機行業(yè)市場運行態(tài)勢與投資戰(zhàn)略咨詢報告
- 性報告2025年魚、蝦、貝、藻類多倍體項目可行性研究分析報告
- 15S202 室內(nèi)消火栓安裝
- 2024年國資委研究中心事業(yè)單位招聘5人歷年(高頻重點復(fù)習提升訓練)共500題附帶答案詳解
- 2023年上海高中學業(yè)水平合格性考試歷史試卷真題(含答案詳解)
- 風力發(fā)電工程施工與驗收規(guī)范
- 2024年個人勞務(wù)承包合同書
- 2024浙江嘉興市海寧高新技術(shù)產(chǎn)業(yè)園區(qū)公開招聘3人重點基礎(chǔ)提升難、易點模擬試題(共500題)附帶答案詳解
- 18 設(shè)計緊急避難路線圖(教案)人美版(北京)(2012)美術(shù)三年級下冊
- GB 9744-2024載重汽車輪胎
- ISO15614-1 2017 金屬材料焊接工藝規(guī)程及評定(中文版)
- 抖音來客商家門店經(jīng)營
- 術(shù)后鎮(zhèn)痛慢性疼痛癌性疼痛診療標準規(guī)范及作業(yè)流程
評論
0/150
提交評論