fork(1) download
  1. #include <bits/stdc++.h>
  2. #define MP make_pair
  3. #define PB push_back
  4. #define int long long
  5. #define st first
  6. #define nd second
  7. #define rd third
  8. #define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
  9. #define RE(i, n) FOR(i, 1, n)
  10. #define FORD(i, a, b) for(int i = (a); i >= (b); --i)
  11. #define REP(i, n) for(int i = 0;i <(n); ++i)
  12. #define VAR(v, i) __typeof(i) v=(i)
  13. #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
  14. #define ALL(x) (x).begin(), (x).end()
  15. #define SZ(x) ((int)(x).size())
  16. using namespace std;
  17. template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
  18. template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
  19. while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
  20. }
  21. #ifdef LOCAL
  22. #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
  23. #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
  24. #else
  25. #define debug(...) (__VA_ARGS__)
  26. #define debugv(x)
  27. #define cerr if(0)cout
  28. #endif
  29. #define next ____next
  30. #define prev ____prev
  31. #define left ____left
  32. #define hash ____hash
  33. typedef long long ll;
  34. typedef long double LD;
  35. typedef pair<int, int> PII;
  36. typedef pair<ll, ll> PLL;
  37. typedef vector<int> VI;
  38. typedef vector<VI> VVI;
  39. typedef vector<ll> VLL;
  40. typedef vector<pair<int, int> > VPII;
  41. typedef vector<pair<ll, ll> > VPLL;
  42.  
  43. template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
  44. template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
  45. template<class T1, class T2>
  46. ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
  47. template<class A, class B, class C> struct Triple { A first; B second; C third;
  48. bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
  49. template<class T> void ResizeVec(T&, vector<int>) {}
  50. template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  51. vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  52. for (T& v : vec) { ResizeVec(v, sz); }
  53. }
  54. typedef Triple<int, int, int> TIII;
  55. template<class A, class B, class C>
  56. ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
  57. template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  58.  
  59. const int P = 1e9 + 7;
  60.  
  61. struct Node {
  62. VI children;
  63. int val;
  64. VI which;
  65. };
  66.  
  67. struct Word {
  68. int beg;
  69. VI let;
  70. int OnPos(int en) {
  71. assert(en - beg < SZ(let));
  72. return let[en - beg];
  73. }
  74. bool IsEnd(int pos) {
  75. return pos - beg == SZ(let) - 1;
  76. }
  77. };
  78. const int N = 1e6 + 5;
  79. Word words[N];
  80. VI which_start[N];
  81. int32_t main() {
  82.  
  83. ios_base::sync_with_stdio(0);
  84. cout << fixed << setprecision(10);
  85. cerr << fixed << setprecision(10);
  86. cin.tie(0);
  87. //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
  88.  
  89. int n;
  90. cin>>n;
  91. string s;
  92. vector<VPII> all;
  93. VPII cur;
  94. int cnt = 0;
  95. while (cin>>s) {
  96. bool should_end = false;
  97. if (s.back() == ')') {
  98. should_end = true;
  99. s.pop_back();
  100. }
  101. if (s.back() == '^' || s.back() == 'v') {
  102. continue;
  103. }
  104. if (s[0] == '(') {
  105. s = s.substr(1);
  106. }
  107. int forb = 0;
  108. if (s[0] == '~') {
  109. forb = 1;
  110. s = s.substr(1);
  111. }
  112. int ind = stoi(s.substr(1));
  113. cur.PB({ind, forb});
  114. if (should_end) {
  115. sort(ALL(cur));
  116. cnt++;
  117. words[cnt].beg = cur[0].st;
  118. for (auto p : cur) {
  119. words[cnt].let.PB(p.nd);
  120. }
  121. which_start[cur[0].st].PB(cnt);
  122. //all.PB(cur);
  123. cur.clear();
  124. }
  125. }
  126. //debug(all);
  127.  
  128. vector<Node> tree{{{}, 1, {}}};
  129. RE (pos, n) {
  130. vector<Node> new_tree;
  131. for (auto x : which_start[pos]) {
  132. tree[0].which.PB(x);
  133. }
  134. new_tree.PB({{}, 0, {}}); // TODO ??
  135. function<void(int, VI)> Dfs = [&](int v, VI links) {
  136. VI ends(2);
  137. VVI which(2);
  138. for (auto word_ind : tree[v].which) {
  139. Word& w = words[word_ind];
  140. REP (add, 2) {
  141. if (w.OnPos(pos) == add) {
  142. if (w.IsEnd(pos)) {
  143. ends[add] = true;
  144. } else {
  145. which[add].PB(word_ind);
  146. }
  147. }
  148. }
  149. }
  150. REP (add, 2) {
  151. if (links[add] == -1 || ends[add]) {
  152. links[add] = -1;
  153. } else {
  154. if (which[add].empty()) {
  155. new_tree[links[add]].val = (new_tree[links[add]].val + tree[v].val) % P;
  156. } else {
  157. new_tree[links[add]].children.PB(SZ(new_tree));
  158. links[add] = SZ(new_tree);
  159. new_tree.PB({{}, tree[v].val, which[add]});
  160. }
  161. }
  162. }
  163. for (auto nei : tree[v].children) {
  164. Dfs(nei, links);
  165. }
  166. };
  167. Dfs(0, {0, 0});
  168. swap(tree, new_tree);
  169. }
  170. int ans = 0;
  171. for (auto x : tree) {
  172. ans = (ans + x.val) % P;
  173. }
  174. cout<<ans<<endl;
  175. return 0;
  176. }
  177.  
Runtime error #stdin #stdout 0.44s 69952KB
stdin
Standard input is empty
stdout
Standard output is empty