zoukankan      html  css  js  c++  java
  • LeetCode-Regular Expression Matching

    '.' Matches any single character.
    '*' Matches zero or more of the preceding element.
    
    The matching should cover the entire input string (not partial).
    
    The function prototype should be:
    bool isMatch(const char *s, const char *p)
    
    Some examples:
    isMatch("aa","a") → false
    isMatch("aa","aa") → true
    isMatch("aaa","aa") → false
    isMatch("aa", "a*") → true
    isMatch("aa", ".*") → true
    isMatch("ab", ".*") → true
    isMatch("aab", "c*a*b") → true

    构造状态机,以正则表达式的长度作为状态,对当前所有激活的状态给予当前字符以及空字符,获取新的激活状态

    class Solution {
    public:
        // 0 acc and next
        // 1 acc loop or next
        // 2 not acc,quit
        // 3 not acc, go next
        int acc(int state,int input){
            if(input==-1000){
                if(state>500)return 1;
                else return 2;
            }
            if(state<500){
                if(state==input)return 0;
                else return 2;
            }
            else if(state==500)return 0;
            else if(state==1500)return 1;
            else{
                if(state-1000==input) return 1;
                else return 2;
            }
    
            return 0;
        }
        bool Process(vector<int>& state,vector<bool>&stateb,vector<bool>&tmpstate,int j,int val){
            int k;
            switch(val){
            case 0:
                tmpstate[j+1]=true;
                return true;
            case 1:
                tmpstate[j]=true;
                k=j+1;
                while(k<stateb.size()){
                    if(state[k-1]<=500)break;
                    tmpstate[k]=true;
                    k++;
                }
                return true;
            default:
                return false;
            }
        }
        bool isMatch(const char *s, const char *p) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function    
            vector<int> state;
            int i=0;
            while(p[i]!=''){
                int st;
                if(p[i]!='.')st=(int)p[i];
                else st=500;
                i++;
                if(p[i]=='*'){st+=1000;i++;}
                state.push_back(st);
            }
            if(state.size()==0){
                if(s[0]=='')return true;
                else return false;
            }
            vector<bool> tmpstate;
            tmpstate.resize(state.size()+1);
            vector<bool> stateb;
            stateb.resize(state.size()+1,false);
            i=0;
            int val=acc(state[0],-1000);
            tmpstate[0]=true;
            Process(state,stateb,tmpstate,0,val);
            stateb=tmpstate;
            bool next;
            while(s[i]!=''){
                next=false;
                for(int j=0;j<tmpstate.size();j++)tmpstate[j]=false;
                for(int j=0;j<state.size();j++){
                    if(stateb[j]){
                         val=acc(state[j],(int)s[i]);
                         bool b=Process(state,stateb,tmpstate,j,val);
                         next=next||b;
                    }
                }
                stateb=tmpstate;
                if(!next)return false;
                for(int j=0;j<state.size();j++){
                    if(stateb[j]){
                         val=acc(state[j],-1000);
                         Process(state,stateb,tmpstate,j,val);
                    }
                }
                stateb=tmpstate;
                i++;
            }
            return stateb[state.size()];
        }
    };
    O(n^2)
  • 相关阅读:
    浏览器渲染机制
    isEmpty 和 isBlank 的用法区别
    Mybatis-plus
    Java8的JVM内存结构
    【面试题】关于线程交替的面试题
    java中的final的作用
    线程池
    六种实现单例模式的方法
    SQL优化常用方法
    HTML学习笔记
  • 原文地址:https://www.cnblogs.com/superzrx/p/3357017.html
Copyright © 2011-2022 走看看