题解 CF1462B 【Last Year's Substring】

来个正则表达式做法,其实也是每个判断,因为只有 55 种可行方案,不用写那么多 if

更多关于正则表达式的使用请看我的题解

代码:

#include <iostream>
#include <string>
#include <regex>
using namespace std;

static regex reg("\\w*2020"), reg2("202\\w*0"), reg3("20\\w*20"), reg4("2\\w*020"), reg5("2020\\w*");

int main()
{
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	for (register int i = 1; i <= n; i++)
	{
		string s;
		cin >> s >> s;
		if (regex_match(s, reg) || regex_match(s, reg2) || regex_match(s, reg3) || regex_match(s, reg4) || regex_match(s, reg5))
		{
			cout << "YES\n";
		}
		else
		{
			cout << "NO\n";
		}
	}
	//system("pause");
	return 0;
}
posted @ 2021-01-26 21:53  HappyBobb  阅读(8)  评论(0)    收藏  举报  来源