# 算法 || dp || 有限状态自动机 #
*leetcode 10
1、暴力递归(Java)
1 public static boolean isMatch(String s, String t) { 2 return match(s, t, 0, 0); 3 } 4 public static boolean match(String s, String t, int i, int j) { 5 if (j == t.length()) return i == s.length(); 6 if(j+1 == t.length() || t.charAt(j+1) != ‘*‘) { 7 return i != s.length() && (t.charAt(j) == s.charAt(i) || t.charAt(j) == ‘.‘) 8 && match(s, t, i+1, j+1); 9 } 10 while(i != s.length() && (t.charAt(j) == s.charAt(i) || t.charAt(j) ==‘.‘)) { 11 if(match(s, t, i, j+2)) return true; 12 i++; 13 } 14 return match(s, t, i, j+2); 15 }
重复操作太多,故考虑改dp。
2、动态规划(Java)
1 public static boolean dpmatch(String s, String t) { 2 int m = s.length(), n = t.length(); 3 if(m == 0 && n ==0) return true; 4 else if (m != 0 && n == 0) return false; 5 boolean [][] dp = new boolean[m+1][n+1]; 6 dp[m][n] = true; 7 for (int j = n-2;j>=0;j = j-2) { 8 if(t.charAt(j+1) == ‘*‘ && t.charAt(j) != ‘*‘) dp[m][j] = true; 9 else break; 10 } 11 if(m == 0) return dp[0][0]; 12 if(t.charAt(n-1) == ‘.‘ || t.charAt(n-1) == s.charAt(m-1)) dp[m-1][n-1] = true; 13 for(int i = m-1; i>=0; i--) { 14 for(int j = n-2; j>=0; j--) { 15 if(t.charAt(j+1) != ‘*‘) { 16 dp[i][j] = (t.charAt(j) == s.charAt(i) || t.charAt(j) == ‘.‘) 17 &&dp[i+1][j+1]; 18 } else { 19 int p = i; 20 while (p != s.length() && (t.charAt(j) == s.charAt(p) || t.charAt(j) ==‘.‘)) { 21 if(dp[p][j+2]) { 22 dp[p][j] = true; 23 break; 24 } 25 p++; 26 } 27 if(dp[i][j] != true) 28 dp[i][j] = dp[p][j+2]; 29 } 30 } 31 } 32 return dp[0][0]; 33 }
3、非确定有限状态机(nondeterministic finite automaton)
详细待补充,代码参照题解。(Java)
1 import java.util.Arrays; 2 import java.util.HashMap; 3 import java.util.HashSet; 4 import java.util.LinkedList; 5 import java.util.List; 6 import java.util.Map; 7 import java.util.Set; 8 9 public class Solutions { 10 class Solution { 11 public boolean isMatch(String s, String p) { 12 State dfa = buildStateChain(p); 13 Set<State> dfaSet = new HashSet<>(); 14 dfaSet.add(dfa); 15 int index = -1; 16 while(++index < s.length()){ 17 char ch = s.charAt(index); 18 String input = String.valueOf(ch); 19 Set<State> newStateSet = new HashSet<>(); 20 for(State state : dfaSet){ 21 List<State> newList = state.changeState(input); 22 if(newList != null){ 23 newStateSet.addAll(newList); 24 } 25 } 26 dfaSet = newStateSet; 27 if(dfaSet.size() <= 0){ 28 break; 29 } 30 } 31 32 boolean isMatch = false; 33 for(State state : dfaSet){ 34 if(state.isEnd()){ 35 isMatch = true; 36 break; 37 } 38 } 39 40 return isMatch; 41 } 42 43 public State buildStateChain(String p){ 44 State start = new State(); 45 start.setStart(true); 46 47 List<State> currentLevelStateList = Arrays.asList(start); 48 for(int i = 0; i < p.length(); i++){ 49 char current = p.charAt(i); 50 if(current == ‘*‘){ 51 continue; 52 } 53 Character next = null; 54 if(i < p.length() - 1){ 55 next = p.charAt(i + 1); 56 } 57 58 List<State> nextLevelStateList = new LinkedList<>(); 59 State nextState = new State(); 60 nextLevelStateList.add(nextState); 61 String currentStr = String.valueOf(current); 62 for(State currentState : currentLevelStateList) { 63 currentState.addNextState(currentStr, nextState); 64 65 // 如果是x* 这种形式的话,状态有个自循环,并且当前状态需要建立一个和下下个状态的关系 66 if (next != null && next.equals(‘*‘)) { 67 nextLevelStateList.add(currentState); 68 nextState.addNextState(currentStr, nextState); 69 } 70 } 71 72 currentLevelStateList = nextLevelStateList; 73 } 74 75 for(State currentState : currentLevelStateList) { 76 currentState.setEnd(true); 77 } 78 79 return start; 80 } 81 82 class State{ 83 private boolean start; 84 private boolean end; 85 86 // 状态转移mapping 87 private Map<String, List<State>> nextMap = new HashMap<>(); 88 89 public List<State> changeState(String input){ 90 List<State> result = new LinkedList<>(); 91 List<State> nextList = nextMap.get(input); 92 if(nextList != null){ 93 result.addAll(nextList); 94 } 95 nextList = nextMap.get("."); 96 if(nextList != null){ 97 result.addAll(nextList); 98 } 99 100 // List<State> forEmptyList = nextMap.get(""); 101 // if(forEmptyList != null){ 102 // result.addAll(forEmptyList); 103 // } 104 105 return result; 106 } 107 108 public boolean isStart() { 109 return start; 110 } 111 112 public void setStart(boolean start) { 113 this.start = start; 114 } 115 116 public boolean isEnd() { 117 return end; 118 } 119 120 public void setEnd(boolean end) { 121 this.end = end; 122 } 123 124 public void addNextState(String input, State next){ 125 List<State> nextList = nextMap.get(input); 126 if(nextList == null){ 127 nextList = new LinkedList<>(); 128 nextMap.put(input, nextList); 129 } 130 nextList.add(next); 131 } 132 133 } 134 }
# Edit : 2020.1.17
原文地址:https://www.cnblogs.com/zzl1209/p/12207181.html
时间: 2024-11-06 09:45:21