Naive String Matching: String Matching
2015-05-19 07:31
267 查看
Naive string matching:
Pseudo code:
Java code:
/** * Created with IntelliJ IDEA. * User: 1O1O * Date: 2015-03-31 * Time: 15:40 PM * :)~ * NAIVE-STRING-MATCH:STRING-MATCH */ public class Main { public static void NAIVE_STRING_MATCHER(String Text, String Pattern){ int n = Text.length(); int m = Pattern.length(); char[] textToChar = Text.toCharArray(); char[] patternToChar = Pattern.toCharArray(); for(int s=0; s<=n-m; s++){ int count = 0; for(int i=0; i<m; i++){ if(patternToChar[i] == textToChar[s+i]) count++; else break; } if(count == m) System.out.println("Pattern occurs with shift: "+(s+1)); } } public static void main(String[] args) { String Text = "bacbababaabcbabababacababbababacabbc"; String Pattern = "ababaca"; NAIVE_STRING_MATCHER(Text, Pattern); } }
Output:
Pattern occurs with shift: 16 Pattern occurs with shift: 27
Reference:
From: 《INTRODUCTION TO ALGORITHMS》THIRD EDITION.
相关文章推荐
- String Matching in the legend
- NSWOJ ~ 1104 ~ BinaryStringMatching(strstr函数)
- acm-Binary String Matching
- NYOJ 5 Binary String Matching
- NYOJ 5 Binary String Matching
- poj 1580 String Matching
- Symbols of String Pattern Matching
- 44. Wildcard Matching (String; Recursion)
- 开开心心学算法--Binary String Matching
- nyoj_5:Binary String Matching
- 【求助】 no matching function for call to `strcpy(std::string&, char*&)'
- Binary String Matching STL秒解-不要太简单
- nyoj-5-Binary String Matching
- nyoj5 Binary String Matching(KMP)
- Nswoj每日一题:Binary String Matching
- String、动态规划——wildcard-matching 通配符匹配
- Binary String Matching
- error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)
- 南阳理工学院OJ - 0005 - Binary String Matching
- 南阳ACM5-Binary String Matching