prestige

ShahnovskiyLab3

Nov 8th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <algorithm>
  2. #include <numeric>
  3. #include <string>
  4. #include <cstring>
  5. #include <set>
  6. #include <map>
  7. #include <vector>
  8. #include <queue>
  9. #include <iostream>
  10. #include <iterator>
  11. #include <cmath>
  12. #include <cstdio>
  13. #include <cstdlib>
  14. #include <sstream>
  15.  
  16. using namespace std;
  17.  
  18. typedef long long ll;
  19. typedef vector<int> VI;
  20. typedef vector<vector<int> > VVI;
  21. typedef pair<int, int> PII;
  22.  
  23.  
  24. #define FOR(i, x, y) for(ll i=x; i<=y; i++)
  25. #define FORD(i, x, y) for (ll i = x; i >= y; --i)
  26. #define REP(i, n) for(ll i=0; i<n; i++)
  27. #define REPD(i, n) for(ll i = n - 1; i >= 0; --i)
  28.  
  29. #define ALL(c) (c).begin(), (c).end()
  30. #define SORT(c) sort(ALL(c))
  31. #define UNIQ(c) SORT(c),(c).resize(unique(ALL(c))-(c).begin())
  32. #define SZ(c) (int)(c).size()
  33.  
  34. #define CLEAR(x) memset(x,0,sizeof x)
  35. #define FILL(x,v) memset(x,v,sizeof x)
  36.  
  37. #define pb push_back
  38. #define mp make_pair
  39. #define X first
  40. #define Y second
  41.  
  42.  
  43.  
  44. const double eps = 1.0e-11;
  45. const double pi = acos(-1.0);
  46. const int ALPHABET_SIZE = 127;
  47.  
  48.  
  49. struct Node {
  50.     map<char, set<int> > links;
  51. };
  52.  
  53. typedef vector<Node> re;
  54.  
  55. void BuildRegexp(const string& pattern, re& regexp) {
  56.     string buf;
  57.     regexp.resize(1);
  58.     REP(i, SZ(pattern)) {
  59.         if (pattern[i] == '*') {
  60.             Node& curr = regexp.back();
  61.             size_t id = regexp.size() - 1;
  62.             REP(j, ALPHABET_SIZE) {
  63.                 curr.links[j].insert(id);
  64.             }
  65.             continue;
  66.         }
  67.         if (pattern[i] == '?') {
  68.             Node& curr = regexp.back();
  69.             size_t id = regexp.size();
  70.             REP(j, ALPHABET_SIZE) {
  71.                 curr.links[j].insert(id);
  72.             }
  73.             regexp.push_back(Node());
  74.             continue;
  75.         }
  76.         if (pattern[i] == ']') {
  77.             Node& curr = regexp.back();
  78.             size_t id = regexp.size();
  79.             REP(j, SZ(buf)) {
  80.                 curr.links[buf[j]].insert(id);
  81.             }
  82.             regexp.push_back(Node());
  83.             buf.clear();
  84.             continue;
  85.         }
  86.         if (pattern[i] != '[') {
  87.             buf.push_back(pattern[i]);
  88.         }
  89.     }
  90. }
  91.  
  92. bool IsValidText(const string& text, const re& regexp) {
  93.     if (regexp.empty())
  94.         return false;
  95.     vector<bool> states(regexp.size());
  96.     states[0] = true;
  97.     REP(i, SZ(text)) {
  98.         vector<bool> nstates(regexp.size());
  99.         REP(j, SZ(regexp)) {
  100.             if (states[j]) {
  101.                 if (!regexp[j].links.count(text[i]))
  102.                     continue;
  103.                 for (set<int>::iterator it = regexp[j].links.at(text[i]).begin();
  104.                      it !=regexp[j].links.at(text[i]).end();
  105.                      ++it) {
  106.                          nstates[*it] = true;
  107.                 }
  108.             }
  109.         }
  110.         nstates[0] = true;
  111.         states = nstates;
  112.     }
  113.     return states.back();
  114. }
  115.  
  116.  
  117. int main() {
  118.     string pattern, text;
  119.     cin >> pattern >> text;
  120.     re regexp;
  121.     BuildRegexp(pattern, regexp);
  122.     cout << IsValidText(text, regexp);
  123.     return 0;
  124. }
  125.  
  126. /*
  127. [abc]?[bc]
  128. abc
  129. [abc]?[b]
  130. abc
  131.  
  132. ?
  133. sdhflsadf
  134.  
  135. [a][b][b][a][b]
  136. ababbab
  137. */
Advertisement
Add Comment
Please, Sign In to add comment