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

跳跃游戏(二)

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;
}
全部评论

相关推荐

xdm怎么说&nbsp;要被拷打了&nbsp;担心是KPI
丹田:面就完了,就当日薪四位数的大佬免费给给你面试。
点赞 评论 收藏
分享
湫湫湫不会java:1.在校经历全删了2.。这些荣誉其实也没啥用只能说,要的是好的开发者不是好好学生3.项目五六点就行了,一个亮点一俩行,xxx技术解决,xxx问题带来xxx提升。第一页学历不行,然后啥有价值的信息也没有,到第二页看到项目了,第一个项目九点,第二个项目像凑数的俩点。总体给人又臭又长,一起加油吧兄弟
点赞 评论 收藏
分享
鬼迹人途:你去投一投尚游游戏,服务器一面,第一个图算法,做完了给你一个策略题,你给出方案他就提出低概率问题,答不上当场给你挂
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-27 15:19
简历上能写3个月吗?
码农索隆:大胆写,主要你能把实习经历包装好,可以看一下我这篇帖子https://www.nowcoder.com/share/jump/4888395581180798063
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务