hdu 4915 Parenthese sequence(贪心,模拟)

14 篇文章 0 订阅
2 篇文章 0 订阅

题目:

Parenthese sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1181    Accepted Submission(s): 485


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
 

题目:给一个字符串,只包含左括号,右括号和问号,问号可以当左括号或右括号来使用,问你这能不能形成一个合法的括号序列,如果可以的话是唯一解还是多解。

思路:我们可以从左向右扫一遍再从右向左扫一遍。从左到右的时候维护出现的右括号的数目r,如果r>扫过的数目/2,说明肯定不能形成合法的序列,如果r*2=扫过的数目,那么扫过的部分形成了一个合法的序列,就将r和扫过的数目清空。扫完后看是否左括号的数目大于扫过的数目/2,从右往左扫也是类似的。如果从左往右扫和从右往左扫都合法,就枚举每个问号,分别试试它作为左括号的情况和右括号的情况,只有一种情况合法的时候把那个位置的问号换成相应的值,两个位置可以就是多解,都不可以的话就是无解。

代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen("in.txt","r",stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判断正负
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的数
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/



char str[1000000+5];
//int mul[1000000+5];
char s[1000000+5];
bool judge()
{
    int len=strlen(str);
    int l=0,r=0,mul=0,ok=1;
    int num=0;
    for(int i=0;i<len;i++)
    {   num++;
        if(num==1&&str[i]=='?')
            str[i]='(';
        if(str[i]=='(')
            l++;

            if(str[i]==')')
        {
            r++;

        }
        if(r>num/2)
            {
                ok=0;
                break;
            }
        if(r*2==num)
        {
            l=r=num=0;
        }
    }
    if(l>num/2)
        ok=0;

    l=r=num=0;
    strcpy(str,s);
    for(int i=len-1;i>=0;i--)
    {
        num++;
        if(num==1&&str[i]=='?')
            str[i]=')';
        if(str[i]=='(')
            l++;
        if(str[i]==')')
            r++;
        if(l>num/2)
        {
            ok=0;
            break;
        }
        if(l*2==num)
        {
            l=r=num=0;
        }
    }
    if(r>num/2)
        ok=0;
    return ok;
}
int main()
{while(scanf("%s",str)!=EOF)
{   strcpy(s,str);
    int len=strlen(str);
    if(judge()==0)
    {
        printf("None\n");
        continue;
    }
    strcpy(str,s);

    for(int i=0;i<len;i++)
    {
        if(str[i]=='?')
        {   strcpy(str,s);
            str[i]='(';
            int flag1=judge();
            strcpy(str,s);
            str[i]=')';
            int flag2=judge();
            if(flag1&&!flag2)
                s[i]='(';
            else
                if(!flag1&&flag2)
                s[i]=')';
            else
                if(flag1&&flag2)
                {
                    printf("Many\n");
                    break;
                }
            else
            {
                printf("None\n");
                break;
            }


        }
        if(i==len-1)
        {
            printf("Unique\n");
        }
    }



}



        return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值