115-Wildcard Matching
2016-07-01 00:27
274 查看
-44. Wildcard Matching My Submissions QuestionEditorial Solution
Total Accepted: 60794 Total Submissions: 341535 Difficulty: Hard
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
有难度,思考整个自动机的运转才能写出无bug的代码
Total Accepted: 60794 Total Submissions: 341535 Difficulty: Hard
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
有难度,思考整个自动机的运转才能写出无bug的代码
class Solution { public: bool isMatch(string s, string p) { return isMatch(s.c_str(),p.c_str()); } bool isMatch(const char *s,const char *p){ bool star = false; const char *str,*ptr; for(str=s,ptr=p;*str!='\0';str++,ptr++){ switch(*ptr){ case '?': break; case '*': star = true; s = str, p =ptr; while(*p=='*')p++; if(*p=='\0')return true; str = s-1; ptr = p -1; break; default: if(*str!=*ptr){ if(!star)return false; s++; str = s-1; ptr = p-1; } } } while(*ptr=='*')ptr++; return *ptr=='\0'; } };
相关文章推荐
- 第二天:识别干系人
- Java编程题目-1:约瑟夫环问题
- 第二天:识别干系人
- bzoj4571【SCOI2016】美味
- 基于android的远程视频监控系统
- redis+mysql架构搭建
- 6月-月总结
- SICP 习题2.42 八皇后问题
- 第一天:制定项目章程
- Javascript算法练习(四)
- 第一天:制定项目章程
- Fragment+自定义RadioButton实现底部导航栏,仿QQ
- mysql 数据库中 常用数据类型,约束
- 漫谈C++:良好的编程习惯与编程要点【1】
- 我的经验:指针和指针的引用
- 4-11 求自定类型元素序列的中位数 (25分)
- Codeforces 577 B Modulo Sum 简单数学+dp
- 数据结构复习笔记(Data Structures & Algorithms in Java, 4th) --Memory Management (Heap & Stack)
- oracle11g 新特性密码延迟验证引发的 血案
- Android中BuildConfig类的那些事<一>