Finite Automaton Matching: String Matching
2015-05-18 08:27
281 查看
Finite automation machine:
Pseudo code:
Java code:
/** * Created with IntelliJ IDEA. * User: 1O1O * Date: 2015-03-31 * Time: 21:29 PM * :)~ * FINITE-AUTOMATON-MATCH:STRING-MATCH */ public class Main { public static int[][] COMPUTE_TRANSITION_FUNCTION(String Pattern, char[] sigma){ int m = Pattern.length(); int n = sigma.length; int[][] delta = new int[m+1] ; for(int q=0; q<=m; q++){ for(int i=0; i<n; i++){ int k = ((m+1)<=(q+2))?(m+1):(q+2); do{ k = k-1; if(k == 0){ delta[q][i] = 0; break; } String Pk = Pattern.substring(0, k); String Pq = Pattern.substring(0, q) + sigma[i]; int Pq_len = Pq.length(); int j; for(j=k-1; j>-1; j--){ if(Pk.charAt(j) == Pq.charAt(--Pq_len)) continue; else break; } if(j == -1){ delta[q][i] = k; break; } }while(true); } } //Format Output: System.out.println("Stat a b c P"); for(int i=0; i<=m; i++){ System.out.print(String.format("%-6s", i)); for(int j=0; j<n; j++){ System.out.print(String.format("%-6s", delta[i][j])); } if(i != m) System.out.print(Pattern.charAt(i)); System.out.println(); } return delta; } public static void FINITE_AUTOMATON_MATCHER(String Text, int[][] delta, int m){ int n = Text.length(); int q = 0; int index; for(int i=0; i<n; i++){ switch(Text.charAt(i)){ case 'a': index = 0; break; case 'b': index = 1; break; case 'c': index = 2; break; default: index = 0; } q = delta[q][index]; if(q == m){ System.out.println("Pattern occurs with shift: "+(i-m+2)); } } } public static void main(String[] args) { String Text = "bacbababaabcbabababacababbababacabbc"; String Pattern = "ababaca"; int m = Pattern.length(); char[] sigma = {'a','b','c'}; int[][] delta = COMPUTE_TRANSITION_FUNCTION(Pattern, sigma); FINITE_AUTOMATON_MATCHER(Text, delta, m); } }
Output:
Stat a b c P 0 1 0 0 a 1 1 2 0 b 2 3 0 0 a 3 1 4 0 b 4 5 0 0 a 5 1 4 6 c 6 7 0 0 a 7 1 2 0 Pattern occurs with shift: 16 Pattern occurs with shift: 27
Reference:
From: 《INTRODUCTION TO ALGORITHMS》THIRD EDITION.
相关文章推荐
- 65 - Valid Number(DFA-Deterministic Finite Automaton)
- String Matching: The Simple way to implement String Matching
- String Matching: The Simple way to implement String Matching
- String matching using Rabin-Karp Algorithm
- 【LeetCode with Python】 Wildcard Matching
- no matching symbolic information found
- KMP matching
- spring依赖注入单元测试:expected single matching bean but found 2
- 算法-字符串匹配(String Matching)-(1)-朴素匹配和Rabin-Karp算法
- Loaded 'NTDLL.DLL', no matching symbolic information found.类似问题~
- 字符串匹配(String matching)[No. 76]
- 出现信息:Loaded 'ntdll.dll', no matching symbolic information found.
- no matching editors or conversion strategy found
- String Matching
- POJ 1580 String Matching 很纠结,代码写了很多,还是过了,再对比一下人家的代码,惭愧
- error C2181: illegal else without matching if
- 关于Wheres Waldo: Matching People in Images of Crowds学习
- VC++调试时提示:no matching symbolic information found
- modelsim的 # (vish-4014) No objects found matching '/*'
- tomcat启动报警告:Setting property 'source' to 'org.eclipse.jst.jee.server:fss2web' did not find a matching property.