题解 | #正则表达式匹配#

跳跃游戏(二)

http://www.nowcoder.com/practice/58e31b785f4b4ced9695dd4fcd60c1ce

动态规划

#include <iostream>
#include <vector>
#include <bitset>

using namespace std;

class solution
{
public:
    int getResult(const string &str, const string &pattern)
    {
        int sLength = str.size(), pLength = pattern.size();
        vector<vector<int>> dp(sLength + 1, vector<int>(pLength + 1, 0));
        dp[0][0] = 1;
        
        for (int i = 0; i <= sLength; i++)
        {
            for (int j = 1; j <= pLength; j++)
            {
                if (pattern[j - 1] != '*')
                {
                    dp[i][j] = i > 0 && dp[i - 1][j - 1] && (str[i - 1] == pattern[j - 1] || pattern[j - 1] == '.');
                }
                else
                {
                    dp[i][j] = (j >= 2 && dp[i][j - 2]) || (i > 0 && (str[i - 1] == pattern[j - 2] || pattern[j - 2] == '.') && dp[i - 1][j]);
                }
            }
        }
        
        return dp[sLength][pLength];
    }
    
};

int main()
{
    string str, pattern;
    getline(cin, str);
    getline(cin, pattern);
    
    solution mSolution;
    int ret = mSolution.getResult(str, pattern);
    if (ret)
    {
        cout << "true" << endl;
    }
    else
    {
        cout << "false" << endl;
    }
    
    return 0;
}
全部评论

相关推荐

2024-12-26 13:00
太原理工大学 Java
会飞的猿:简历没啥大问题啊,感觉是缺少了实习经历。多投投先找个中小厂过渡一下吧
点赞 评论 收藏
分享
EEbond:给北邮✌️跪了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务