leetcode 10. Regular Expression Matching

Implement regular expression matching with support for '.' and '*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.

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", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true
这题挺难的,虽然我写的迭代算法效率还算高,但他妈的又臭又长,实在懒得看第二遍,就让它过了吧。


445 / 445 test cases passed.
Status: Accepted
Runtime: 28 ms

class Solution {
	bool do_once(vector<pair<string, string>>&candi, set<pair<string, string>>&hist)
	{
		vector<pair<string, string>>newcandi;
		for (int i = candi.size()-1; i >=0; i--)
		{
			string ss = candi[i].first;
			string pp = candi[i].second;
			if (ss == pp)
				return true;
			if (pp.find('.') == string::npos&&pp.find('*') == string::npos)
				continue;
			int hh = 0;
			int cc1 = 0;
			while (hh < pp.length() && pp[hh] == '.' || pp[hh] == '*')
			{
				cc1 += pp[hh] == '*' ? 1 : 0;
				hh++;
			}
			if (hh == pp.length())
			{
				if (cc1>0&&ss.length() >= pp.length() - 2 * cc1)
					return true;
				if (cc1 == 0 && ss.length() == pp.length())
					return true;
				continue;
			}
			int hr = hh;
			while (hr < pp.length() && pp[hr] != '.'&&pp[hr] != '*')
				hr++;
			if (hr < pp.length() && pp[hr] == '*')
			{
				string str = string(pp.begin() + hh, pp.begin() + hr - 1);
				string ggg = string(pp.begin() + hr + 1, pp.end());
				int bbb = 0;
				if (hh == 0)
				{
					while (ss.length() >= str.length()+bbb)
					{
						string str1 = str + string(bbb, pp[hr - 1]);
						if (string(ss.begin(), ss.begin() + str1.length()) == str1)
						{
							string gg = string(ss.begin() + str1.length(), ss.end());
							if (hist.find(pair<string, string>(gg, ggg)) == hist.end())
							{
								hist.insert(pair<string, string>(gg, ggg));
								newcandi.push_back(pair<string, string>(gg, ggg));
							}
						}
						else
							break;
						bbb++;
					}
				}
				else
				{
					if (cc1 > 0)
						while (true)
						{
							string str1 = str + string(bbb, pp[hr - 1]);
							int pos = ss.find(str1);
							if (pos != string::npos)
							{
								while (pos != string::npos)
								{
									if (pos >= hh - 2 * cc1)
									{
										string gg = string(ss.begin() + pos+str1.length(), ss.end());
										if (hist.find(pair<string, string>(gg, ggg)) == hist.end())
										{
											hist.insert(pair<string, string>(gg, ggg));
											newcandi.push_back(pair<string, string>(gg, ggg));
										}
									}
									pos = ss.find(str1, pos + 1);
								}
							}
							else
								break;
							bbb++;
						}
					else
					{
						while (hh + str.length() + bbb <= ss.length())
						{
							string str1 = str + string(bbb, pp[hr - 1]);
							if (string(ss.begin() + hh, ss.begin() +hh+ str1.length()) == str1)
							{
								string gg = string(ss.begin() + hh + str1.length(), ss.end());
								if (hist.find(pair<string, string>(gg, ggg)) == hist.end())
								{
									hist.insert(pair<string, string>(gg, ggg));
									newcandi.push_back(pair<string, string>(gg, ggg));
								}
							}
							else
								break;
							bbb++;
						}
					}
				}
			}
			else
			{
				string str = string(pp.begin() + hh, pp.begin() + hr);
				string ggg = string(pp.begin() + hr, pp.end());
				if (hh == 0)
				{
					if (string(ss.begin(), ss.begin() + str.length()) == str)
					{
						string gg = string(ss.begin() + str.length(), ss.end());
						if (hist.find(pair<string, string>(gg, ggg)) == hist.end())
						{
							hist.insert(pair<string, string>(gg, ggg));
							newcandi.push_back(pair<string, string>(gg, ggg));
						}
					}
				}
				else
				{
					if (cc1 > 0)
					{
						int pos = ss.find(str);
						if (pos != string::npos)
						{
							while (pos != string::npos)
							{
								if (pos >= hh - 2 * cc1)
								{
									string gg = string(ss.begin() + pos + str.length(), ss.end());
									if (hist.find(pair<string, string>(gg, ggg)) == hist.end())
									{
										hist.insert(pair<string, string>(gg, ggg));
										newcandi.push_back(pair<string, string>(gg, ggg));
									}
								}
								pos = ss.find(str, pos + 1);
							}
						}
					}
					else
					{
						if (string(ss.begin() + hh, ss.begin() + hh + str.length()) == str)
						{
							string gg = string(ss.begin() + hh + str.length(), ss.end());
							if (hist.find(pair<string, string>(gg, ggg)) == hist.end())
							{
								hist.insert(pair<string, string>(gg, ggg));
								newcandi.push_back(pair<string, string>(gg, ggg));
							}
						}
					}
				}
			}
		}
		candi = newcandi;
		return false;
	}
public:
	bool isMatch(string s, string p) {
		if (s == p)
			return true;
		int pos = p.find('*');
		while (pos != string::npos)
		{
			if (pos + 2 < p.length() && p[pos - 1] == p[pos + 1] && p[pos] == p[pos + 2])
				p.erase(pos + 1, 2);
			else
				pos = p.find('*', pos + 1);
		}
		vector<pair<string, string>>candi;
		candi.push_back(pair<string, string>(s, p));
		bool f = false;
		set<pair<string, string>>hist;
		while (!candi.empty()&&!f)
		{
			f = do_once(candi,hist);
		}
		return f;
	}
};


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值