LeetCode - 44. Wildcard Matching

简介: 44. Wildcard Matching  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个字符串和一个自动机,问你自动机可否接收这个字符串.

44. Wildcard Matching 

Problem's Link

 ----------------------------------------------------------------------------

Mean: 

给你一个字符串和一个自动机,问你自动机可否接收这个字符串.

analyse:

由于自动机的模式很简单,所以可以直接模拟.

Time complexity: O(N)

 

view code

/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
*       Author: crazyacking
*       Date  : 2016-03-06-19.05
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long( LL);
typedef unsigned long long( ULL);
const double eps( 1e-8);

class Solution
{
public :
    bool isMatch( string s , string p)
    {
        int star_ =- 1 , ts;
        int ss( 0 ),pp( 0);
        while(ss <s . length())
        {
            if(s [ss ] ==p [pp ] || p [pp ] == '?')
            {
                ++ss , ++pp;
                continue;
            }
            if(p [pp ] == '*')
            {
                star_ =pp ++ , ts =ss;
                continue;
            }
            if( star_ !=- 1)
            {
               pp = star_ + 1 ,ss =++ ts;
                continue;
            }
            return false;
        }
        while(p [pp ] == '*')
            ++pp;
        return (pp ==p . length());
    }
};

int main()
{
    string s1 , s2;
    while( cin >> s1 >> s2)
    {
        Solution solution;
        bool res = solution . isMatch( s1 , s2);
        if( res)
            cout << "True" << endl;
        else
            cout << "False" << endl;
    }
    return 0;
}
/*

*/
目录
相关文章
|
索引 Python
LeetCode 44. Wildcard Matching
给定输入字符串s和模式串(p),实现通配符模式匹配支持'?' 和'*'.
75 0
|
C++
[LeetCode] Wildcard Matching
Well, so many people has tried to solve this problem using DP. And almost all of them get TLE (if you see a C++ DP solution that gets accepted, please let me know ^_^).
1015 0
|
12天前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
26 6
|
12天前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
26 4
|
12天前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
39 2
|
12天前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
15 7
|
12天前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
12 4
|
13天前
|
Python
【Leetcode刷题Python】剑指 Offer 22. 链表中倒数第k个节点
Leetcode题目"剑指 Offer 22. 链表中倒数第k个节点"的Python解决方案,使用双指针法找到并返回链表中倒数第k个节点。
31 5
|
12天前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
22 3