|
| 1 | +#include <algorithm> |
| 2 | +#include <iostream> |
| 3 | +#include <sstream> |
| 4 | +#include <string> |
| 5 | +#include <vector> |
| 6 | +#include <queue> |
| 7 | +#include <set> |
| 8 | +#include <map> |
| 9 | +#include <cstdio> |
| 10 | +#include <cstdlib> |
| 11 | +#include <cctype> |
| 12 | +#include <cmath> |
| 13 | +#include <string> |
| 14 | +#include <cstring> |
| 15 | +using namespace std; |
| 16 | + |
| 17 | +#define REP(i,n) for(int i=0;i<(n);++i) |
| 18 | +#define FOR(i,a,b) for(int i=(a);i<=(b);++i) |
| 19 | +#define RFOR(i,a,b) for(int i=(a);i>=(b);--i) |
| 20 | +#define FOREACH(it,c) for(typeof((c).begin())it=(c).begin();it!=(c).end();++it) |
| 21 | +#define CLR(x) memset((x),0,sizeof((x))) |
| 22 | +#define MP make_pair |
| 23 | +#define MPI make_pair<int, int> |
| 24 | +#define PB push_back |
| 25 | +typedef long long LL; |
| 26 | +typedef vector<int> VI; |
| 27 | +typedef vector<string> VS; |
| 28 | +typedef pair<int, int> PI; |
| 29 | + |
| 30 | +class Solution { |
| 31 | +public: |
| 32 | + bool isMatch(const char *s, const char *p) { |
| 33 | + string ss = string(s); |
| 34 | + string pp = string(p); |
| 35 | + int ls = ss.length(), lp = pp.length(); |
| 36 | + int i1 = 0, i2 = 0; |
| 37 | + int star = -1, ts; |
| 38 | + while (i1 < ls) { |
| 39 | + if (i2 < lp) { |
| 40 | + if (ss[i1] == pp[i2] || pp[i2] == '?') { |
| 41 | + ++i1; |
| 42 | + ++i2; |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + if (pp[i2] == '*') { |
| 47 | + star = i2++; |
| 48 | + ts = i1; |
| 49 | + continue; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (star != -1) { |
| 54 | + i2 = star + 1; |
| 55 | + i1 = ++ts; |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + while (i2 < lp && pp[i2] == '*') ++i2; |
| 63 | + return i2 == lp; |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +int main() { |
| 68 | + Solution s = Solution(); |
| 69 | + cout << s.isMatch("aa", "a") << endl; |
| 70 | + cout << s.isMatch("aa", "aa") << endl; |
| 71 | + cout << s.isMatch("aaa", "aa") << endl; |
| 72 | + cout << s.isMatch("aa", "*") << endl; |
| 73 | + cout << s.isMatch("aa", "a*") << endl; |
| 74 | + cout << s.isMatch("ab", "?*") << endl; |
| 75 | + cout << s.isMatch("aab", "c*a*b") << endl; |
| 76 | + return 0; |
| 77 | +} |
0 commit comments