bobo would like to replace each "?" with "(" or ")" so that the string is valid (defined as follows). Check if the way of replacement can be uniquely determined.
Note:
An empty string is valid.
If S is valid, (S) is valid.
If U,V are valid, UV is valid.
A string s1s2…sn (1≤n≤106).
If there is unique valid string, print "Unique". If there are no valid strings at all, print "None". Otherwise, print "Many".
Unique Many None
思路:亂搞的、、、
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1000800
char str[maxn],str2[maxn],S[maxn];
inline int min(int a,int b)
{
return a>b?b:a;
}
int fuck(char * s)
{
int left = 0,right = 0,res = 0;
int len = strlen(s);
if(len & 1) return 0;
bool flag = true;
int last = 0;
if(s[0] == ')') return 0;
else left++;
for(int i = 1;i < len-1;i++)
{
if(s[i] == '(') left++;
else if(s[i] == ')') right++;
else res++;
if(left+res < right) return 0;
if(left + res == right || left + res == right+1)
{
last = i;//last之前全部確定
left += res;
res = 0;
}
}
if(s[len-1] == '(')
return 0;
else
{
right++;
if(left + res == right) last = len-1;
if(right + res == left) last = len-1;
}
if(left + res < right || right + res < left) return 0;
else if(last == len-1) return 1;
else return 2;
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%s",str)!=EOF)
{
int ans = 3;
ans = min(ans,fuck(str));
int len = strlen(str);
for(int i = len-1;i >= 0;i--)//反過來也要跑一次。
{
char a;
if(str[i] == '(') a = ')';
else if(str[i] == ')') a = '(';
else a = '?';
str2[len-1-i] = a;
}
str2[len] = '\0';
int hehe = fuck(str2);
ans = min(ans,hehe);
if(ans == 0) cout << "None" << endl;
else if(ans == 1) cout << "Unique" << endl;
else cout << "Many" << endl;
}
}