#include<bits/stdc++.h> using namespace std; # ifdef DEB # define debug(...) fprintf(stderr, __VA_ARGS__) # else # define debug(...) #endif #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define LD long double #define PII pair<int, int> #define PLL pair<LL, LL> #define pb pop_back #define VI vector<int> #define VPI vector<PII> #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define ALL(x) (x).begin(), (x).end() #define siz(V) ((int)V.size()) const int M=1e6+5; const LL mod=1e9+7; struct Node { bool end; Node* s[2]; int val; }; bool B[M]; int n, l, dp[M]; char C[20000000]; Node * T[M], * nothing; void addtotrie(Node* root, int start, int end) { if(start<end) { root->end=true; return; } //debug("adding %d %d\n", start, end); if(root->s[B[start]]==NULL) root->s[B[start]]=new Node(); addtotrie(root->s[B[start]], start-1, end); } void getclause(int ind) { int mini=n, maxi=0; while(C[ind]!=')') { bool boo=1; ind++; if(C[ind]=='~') { boo=0; ind++; } ind++; int num=atoi(C+ind); //debug("%d %d\n", ind, num); while(C[ind]!=')' && C[ind]!='v') ind++; if(C[ind]=='v') ind++; mini=min(mini, num); maxi=max(maxi, num); B[num]=boo; } //debug("clause from %d to %d\n", mini, maxi); addtotrie(T[maxi], maxi, mini); } void parseinput() { REP(i, l) if(C[i]=='(') getclause(i); } void dfs(Node* root, Node* mir, int ind) { //debug("in: %d %d %d\n", root->val, mir->val, ind); if(mir->val==0 || root->end) return; REP(i, 2) { //if(i==0) // debug("hmm %d %d %d\n", root->s[i]==NULL, mir->s[i]==NULL, ind); if(root->s[i]==NULL) { if(mir->s[i]==NULL) root->val+=dp[ind-1]; else { root->s[i]=mir->s[i]; root->val+=mir->s[i]->val; } } else { if(mir->s[i]==NULL) dfs(root->s[i], nothing, ind-1); else dfs(root->s[i], mir->s[i], ind-1); root->val+=root->s[i]->val; } } if(root->val>=mod) root->val-=mod; //debug("val= %d\n", root->val); } void go(int ind) { REP(i, 2) { if(T[ind]->s[i]==NULL) { T[ind]->val+=dp[ind-1]; if(ind>1) T[ind]->s[i]=T[ind-1]; } else { //debug("going %d -> %d\n", ind, i); dfs(T[ind]->s[i], T[ind-1], ind-1); T[ind]->val+=T[ind]->s[i]->val; } } if(T[ind]->val>=mod) T[ind]->val-=mod; dp[ind]=T[ind]->val; } int main() { nothing=new Node(); nothing->val=1; scanf("%d\n", &n); REP(i, n+1) T[i]=new Node(); dp[0]=1; fgets(C, 1e9, stdin); l=strlen(C)-1; //debug("%s", C); parseinput(); RE(i, n) go(i); printf("%d\n", dp[n]); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | #include<bits/stdc++.h> using namespace std; # ifdef DEB # define debug(...) fprintf(stderr, __VA_ARGS__) # else # define debug(...) #endif #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define LD long double #define PII pair<int, int> #define PLL pair<LL, LL> #define pb pop_back #define VI vector<int> #define VPI vector<PII> #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define ALL(x) (x).begin(), (x).end() #define siz(V) ((int)V.size()) const int M=1e6+5; const LL mod=1e9+7; struct Node { bool end; Node* s[2]; int val; }; bool B[M]; int n, l, dp[M]; char C[20000000]; Node * T[M], * nothing; void addtotrie(Node* root, int start, int end) { if(start<end) { root->end=true; return; } //debug("adding %d %d\n", start, end); if(root->s[B[start]]==NULL) root->s[B[start]]=new Node(); addtotrie(root->s[B[start]], start-1, end); } void getclause(int ind) { int mini=n, maxi=0; while(C[ind]!=')') { bool boo=1; ind++; if(C[ind]=='~') { boo=0; ind++; } ind++; int num=atoi(C+ind); //debug("%d %d\n", ind, num); while(C[ind]!=')' && C[ind]!='v') ind++; if(C[ind]=='v') ind++; mini=min(mini, num); maxi=max(maxi, num); B[num]=boo; } //debug("clause from %d to %d\n", mini, maxi); addtotrie(T[maxi], maxi, mini); } void parseinput() { REP(i, l) if(C[i]=='(') getclause(i); } void dfs(Node* root, Node* mir, int ind) { //debug("in: %d %d %d\n", root->val, mir->val, ind); if(mir->val==0 || root->end) return; REP(i, 2) { //if(i==0) // debug("hmm %d %d %d\n", root->s[i]==NULL, mir->s[i]==NULL, ind); if(root->s[i]==NULL) { if(mir->s[i]==NULL) root->val+=dp[ind-1]; else { root->s[i]=mir->s[i]; root->val+=mir->s[i]->val; } } else { if(mir->s[i]==NULL) dfs(root->s[i], nothing, ind-1); else dfs(root->s[i], mir->s[i], ind-1); root->val+=root->s[i]->val; } } if(root->val>=mod) root->val-=mod; //debug("val= %d\n", root->val); } void go(int ind) { REP(i, 2) { if(T[ind]->s[i]==NULL) { T[ind]->val+=dp[ind-1]; if(ind>1) T[ind]->s[i]=T[ind-1]; } else { //debug("going %d -> %d\n", ind, i); dfs(T[ind]->s[i], T[ind-1], ind-1); T[ind]->val+=T[ind]->s[i]->val; } } if(T[ind]->val>=mod) T[ind]->val-=mod; dp[ind]=T[ind]->val; } int main() { nothing=new Node(); nothing->val=1; scanf("%d\n", &n); REP(i, n+1) T[i]=new Node(); dp[0]=1; fgets(C, 1e9, stdin); l=strlen(C)-1; //debug("%s", C); parseinput(); RE(i, n) go(i); printf("%d\n", dp[n]); return 0; } |