Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4915
Given a sequence, ()? Components, where? It can represent (OR), and there is one, multiple, or no matching.
From left to right, fill n/2 left brackets with priority, and continue to fill the right brackets. If there is a conflict in the process (the number of right parentheses exceeds the number of left parentheses), it is none. Otherwise, you must determine whether the unique solution is a multiple solution.
After the records are exactly filled with n/2 left brackets during the previous traversal, the positions of the right brackets are forcibly set to left parentheses for the first time, and the number of question marks is reduced by one, if the general rule can be run, multiple solutions are provided. Otherwise, a single solution is provided.
#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <queue>#include <vector>#include <iostream>#include <algorithm>using namespace std;#define RD(x) scanf("%d",&x)#define RD2(x,y) scanf("%d%d",&x,&y)#define clr0(x) memset(x,0,sizeof(x))typedef long long LL;int n;char s[1000010];int a[1000005],b[1000005],tot,t,m;int main(){ while (scanf("%s",s)!=EOF){ n=strlen(s); int i,j,k; tot = m = t = 0; if(n%2){puts("None");continue;} for(int i = 0;i < n;++i){ if(s[i] == '('){ a[i] = b[i] = 1; tot++; }else if(s[i] == ')') a[i] = b[i] = -1; else{ a[i] = b[i] = 0; m++; } } t = n-tot-m; if(tot > n/2 || t > n/2){puts("None");continue;} t = n/2 - tot; int pos = -1; for (k = 0,i = 0;i < n;++i){ if (a[i] == 0){ if (t) --t,k++; else{ if(pos == -1) pos = i; k--; } } else k+=a[i]; if (k < 0){ puts("None"); goto end; } } if(k != 0){ puts("None"); continue; } t = n/2 - tot; if(t == m || t == 0){ puts("Unique"); continue; } --t; for(b[pos] = 1,k = 0,i = 0;i<n;++i){ if(b[i] == 0){ if (t) --t,k++; else k--; } else k += b[i]; if (k < 0){ puts("Unique"); goto end; } } puts("Many"); end:; } return 0;}
HDU 4915 bracket matching + clever Simulation