您的位置:首页 > 产品设计 > UI/UE

hdu 4915 Parenthese sequence

2014-08-05 19:09 344 查看


Parenthese sequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 85 Accepted Submission(s): 28



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




Author

Xiaoxu Guo (ftiasch)



Source

2014 Multi-University Training Contest 5



题意:给你一串带有‘(’,‘)’和‘?’的字符串,‘?’可以变为任意括号,判断该串是否符合条件。若符合,判断是否唯一。

思路:先把特殊情况排除掉:长度为奇数,任意一种括号大于一半。然后这么搞:

令一半长度为half,左括号数为lsum,则将前 half - lsum 个‘?’变为‘(’,剩余的变为‘)’。这是极端情况,若这样也不符合,那就一定找不到符合的。若符合,则将最后一个左括号与第一个右括号(都是由‘?’变过去的,若其中一个没有,则解唯一)调换,若这也符合条件,说明解不唯一;否则解唯一

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <functional>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
//#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF 1e9
#define MAXN 21
const int maxn = 1000005;
//#define mod 1000000007
#define eps 1e-7
#define pi 3.1415926535897932384626433
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define scan(n) scanf("%d",&n)
#define scanll(n) scanf("%I64d",&n)
#define scan2(n,m) scanf("%d%d",&n,&m)
#define scans(s) scanf("%s",s);
#define ini(a) memset(a,0,sizeof(a))
#define out(n) printf("%d\n",n)
//ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b);}
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
char s[maxn];
bool check()
{
	int f = 0;
	int len = strlen(s);
	rep(i,len)
	{
		if(s[i] == '(') f ++;
		else f--;
		if(f < 0) return false;
	}
	if(f != 0) return false;
	return true;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	//	 freopen("out.txt","w",stdout);
#endif  
	while(~scanf("%s",s))
	{
		int len = strlen(s);
		if(len & 1)
		{
			puts("None");
			continue;
		}
		int half = len / 2;
		int lsum = 0,rsum = 0;
		bool ok = 1;
		rep(i,len)
		{
			if(s[i] == '(') lsum ++;
			if(s[i] == ')') rsum ++;
		}
		if(lsum > half || rsum > half)
		{
			puts("None");
			continue;
		}
		int rest = half - lsum;
		int lastLeft = -1,firstRight = -1;
		rep(i,len)
		{
			if(s[i] == '?')
			{
				if(rest > 0)
				{
					rest --;
					s[i] = '(';
					if(rest == 0) lastLeft = i;
				}
				else
				{
					s[i] = ')';
					if(firstRight == -1) firstRight = i;
				}
			}
		}
		if(!check()) puts("None");
		else
		{
			if(lastLeft == -1 || firstRight == -1)
			{
				puts("Unique");
				continue;
			}
			swap(s[lastLeft],s[firstRight]);
			if(check()) puts("Many");
			else puts("Unique");
		}
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: