Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <numeric>
- #include <string>
- #include <cstring>
- #include <set>
- #include <map>
- #include <vector>
- #include <queue>
- #include <iostream>
- #include <iterator>
- #include <cmath>
- #include <cstdio>
- #include <cstdlib>
- #include <sstream>
- using namespace std;
- typedef long long ll;
- typedef vector<int> VI;
- typedef vector<vector<int> > VVI;
- typedef pair<int, int> PII;
- #define FOR(i, x, y) for(ll i=x; i<=y; i++)
- #define FORD(i, x, y) for (ll i = x; i >= y; --i)
- #define REP(i, n) for(ll i=0; i<n; i++)
- #define REPD(i, n) for(ll i = n - 1; i >= 0; --i)
- #define ALL(c) (c).begin(), (c).end()
- #define SORT(c) sort(ALL(c))
- #define UNIQ(c) SORT(c),(c).resize(unique(ALL(c))-(c).begin())
- #define SZ(c) (int)(c).size()
- #define CLEAR(x) memset(x,0,sizeof x)
- #define FILL(x,v) memset(x,v,sizeof x)
- #define pb push_back
- #define mp make_pair
- #define X first
- #define Y second
- const double eps = 1.0e-11;
- const double pi = acos(-1.0);
- const int ALPHABET_SIZE = 127;
- struct Node {
- map<char, set<int> > links;
- };
- typedef vector<Node> re;
- void BuildRegexp(const vector<int>& pattern, re& regexp) {
- string buf;
- regexp.resize(1);
- REP(i, SZ(pattern)) {
- if (pattern[i] == '*' + 200) {
- Node& curr = regexp.back();
- size_t id = regexp.size() - 1;
- REP(j, ALPHABET_SIZE) {
- curr.links[j].insert(id);
- }
- continue;
- }
- if (pattern[i] == '?' + 200) {
- Node& curr = regexp.back();
- size_t id = regexp.size();
- REP(j, ALPHABET_SIZE) {
- curr.links[j].insert(id);
- }
- regexp.push_back(Node());
- continue;
- }
- if (pattern[i] == ']') {
- Node& curr = regexp.back();
- size_t id = regexp.size();
- REP(j, SZ(buf)) {
- curr.links[buf[j]].insert(id);
- }
- regexp.push_back(Node());
- buf.clear();
- continue;
- }
- if (pattern[i] != '[') {
- buf.push_back(pattern[i]);
- }
- }
- }
- bool IsValidText(const string& text, const re& regexp) {
- if (regexp.empty())
- return false;
- vector<bool> states(regexp.size());
- states[0] = true;
- REP(i, SZ(text)) {
- vector<bool> nstates(regexp.size());
- REP(j, SZ(regexp)) {
- if (states[j]) {
- if (!regexp[j].links.count(text[i]))
- continue;
- for (set<int>::iterator it = regexp[j].links.at(text[i]).begin();
- it !=regexp[j].links.at(text[i]).end();
- ++it) {
- nstates[*it] = true;
- }
- }
- }
- if (nstates.back() == true)
- return true;
- nstates[0] = true;
- states = nstates;
- }
- return states.back();
- }
- vector<int> Adjustment(string text) {
- vector<int> rez;
- for (int i = 0; i < text.length(); i++) {
- if (text[i] == '\\')
- continue;
- if (isalpha(text[i])) {
- rez.push_back('[');
- rez.push_back(text[i]);
- rez.push_back(']');
- }
- else if (i && text[i - 1] != '\\' && text[i] == '*') {
- rez.push_back(int('*') + 200);
- } else if (i && text[i - 1] != '\\' && text[i] == '?') {
- rez.push_back(int('?') + 200);
- } else {
- rez.push_back('[');
- rez.push_back(text[i]);
- rez.push_back(']');
- }
- }
- return rez;
- }
- int main() {
- string pattern, text;
- cin >> pattern >> text;
- re regexp;
- BuildRegexp(Adjustment(pattern), regexp);
- cout << IsValidText(text, regexp);
- return 0;
- }
- /*
- [abc]?[bc]
- abc
- [abc]?[b]
- abc
- ?
- sdhflsadf
- [a][b][b][a][b]
- ababbab
- */
Advertisement
Add Comment
Please, Sign In to add comment