hdu 4915 Parenthese sequence

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 s 1s 2…s n (1≤n≤10 6).
 

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
 

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

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

令一半长度为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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值