字符串匹配

1.正则表达式匹配

'.' 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

这道题用DP可以解决,我们设一个字符串s长度为m,另一个字符串p长度为n

则创建数组dp[m+1][n+1],dp[i][j]表示s字符串[0,i)是否能匹配p字符串[0,j),接下来就是三种情况

class Solution {
public:
    bool isMatch(string s, string p) {
        int m = s.size(),n=p.size();
        vector<vector<bool>>dp(m+1,vector<bool>(n+1,false));
        dp[0][0]=true;
        for(int i =0;i<=m;++i)
            for(int j = 1;j<=n;++j)
            {
                if(p[j-1]=='*')
                    dp[i][j]=dp[i][j-2]||(i>0&&(s[i-1]==p[j-2]||p[j-2]=='.')&&dp[i-1][j]); //1.*匹配数量为0     2.s[i-1]与p[j-2]匹配,若匹配,则 dp[i][j]=dp[i-1][j]
                else
                    dp[i][j]=i>0&&(s[i-1]==p[j-1]||p[j-1]=='.')&&dp[i-1][j-1]; //3.若不是'*',则只有一种情况,即s[i-1]==p[j-1]||p[j-1]=='.'
            }
        return dp[m][n];
    }
};

2.通识符匹配

Implement wildcard pattern matching with support for '?' and '*'.

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).

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", "") → true
isMatch("aa", "a
") → true
isMatch("ab", "?") → true
isMatch("aab", "c
a*b") → false

第一种方法是动态规划,动态方程很好写出

class Solution {
public:
    bool isMatch(string s, string p) {
        int m = s.size(),n= p.size();
        vector<vector<bool>>dp(m+1,vector<bool>(n+1,false));
        dp[0][0]=1;
        for(int i = 0;i<=m;++i)
            for(int j = 1;j<=n;++j)
            {
                if(p[j-1]=='*')
                    dp[i][j]=(i>0&&dp[i-1][j])||dp[i][j-1];
                else 
                    dp[i][j]=i>0&&(p[j-1]=='?'||p[j-1]==s[i-1])&&dp[i-1][j-1];
            }
        return dp[m][n];
    }
};

第二种方法是贪心

class Solution {
public:
    bool isMatch(string s, string p) {
        int star=-1,i=0,j=0,ss=0;
        while(i<s.size())
        {
            if(s[i]==p[j]||p[j]=='?')
            {
                i++;
                j++;
            }
            else if(p[j]=='*')
            {
                star=j++;
                ss=i;
            }
            else if(star!=-1)
            {
                j=star+1;
                i=++ss;
            }
            else
                return false;
        }
        while(p[j]=='*')j++;
        return j==p.size();
    }
};

贪心方法比动态规划快上不少,因为贪心方法可以在遍历的过程中匹配不成功即终止算法

而动态规划需要遍历完,如果数据比较强的话动态规划容易卡住

3.子序列的匹配

Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of S.

Example :
Input:
S = "abcde"
words = ["a", "bb", "acd", "ace"]
Output: 3
Explanation: There are three words in words that are a subsequence of S: "a", "acd", "ace".

第一种方法很容易想出,即贪心的算法,但是被很长且重复的子字符串给卡住时间了,所以用map保存已匹配过的字符串

class Solution {
public:
    int numMatchingSubseq(string S, vector<string>& words) {
        int res=0;
        map<string,int>m;
        for(int i = 0;i<words.size();++i)
        {
            if(m.find(words[i])!=m.end())
            {
                res+=m[words[i]];
                continue;
            }
            int j,k;
            for(j =0,k=0;k<S.size()&&j<words[i].size();++k)
            {
                if(words[i][j]==S[k])
                    j++;
            }
            if(j==words[i].size())
            {
                res++;
                m[words[i]]=1;
            }
            else
                m[words[i]]=0;
        }
        return res;
    }
};

第二种为前缀的想法,匹配了前缀,去掉第一位继续放入queue,随后继续匹配,若长度为1,则结果加1

class Solution {
public:
    int numMatchingSubseq(string S, vector<string>& words) {
        int res=0;
        map<char,queue<string>>m;
        for(auto &word:words)
            m[word[0]].push(word);
        for(auto &c:S)
        {
            if(m.count(c))
            {
                int n = m[c].size();
                while(n--)
                {
                    string word=m[c].front();
                    m[c].pop();
                    if(word.size()==1)
                        res++;
                    else
                        m[word[1]].push(word.substr(1));
                }
            }
        }
        return res;
    }
};

转载于:https://www.cnblogs.com/vhyz/p/8682174.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值