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

跳跃游戏(二)

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-27 10:21
已编辑
海南师范大学 媒介策划
到我怀里来:身高体重住址这些就别写了,留几个关键的就行,工作经历突出重点写详细点
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务