• 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)
  • 相关阅读:
    QTP 11.05下载并完成+皴
    ZOJ Monthly, June 2014 月赛BCDEFGH题题解
    Linux makefile 教程 很具体,且易懂
    oracle中imp命令具体解释
    html5实现摇一摇
    AfxMessageBox和MessageBox差别
    Android传感器概述(六)
    线性代数之矩阵与坐标系的转换
    測试新浪微博@小冰 为代码机器人的一些方法
    破解中国电信华为无线猫路由(HG522-C)自己主动拨号+不限电脑数+iTV
  • 原文地址:https://www.cnblogs.com/superzrx/p/3357017.html
Copyright © 2020-2023  润新知