HDU4915--Parenthese sequence

Problem Description
bobo found an ancient string. The string contains only three charaters -- "(", ")" and "?".

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.

Input
The input consists of several tests. For each tests:

A string s1s2…sn (1≤n≤106).

Output
For each tests:

If there is unique valid string, print "Unique". If there are no valid strings at all, print "None". Otherwise, print "Many".

Sample Input
?? ???? (??

Sample Output
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;
	}
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章