题目:

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
2014 Multi-University Training Contest 5

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

思路:我们可以从左向右扫一遍再从右向左扫一遍。从左到右的时候维护出现的右括号的数目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 HOMEfreopen("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 HOMEint 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]='(';elseif(!flag1&&flag2)s[i]=')';elseif(flag1&&flag2){printf("Many\n");break;}else{printf("None\n");break;}}if(i==len-1){printf("Unique\n");}}}return 0;
}

hdu 4915 Parenthese sequence(贪心,模拟)相关推荐

  1. hdu 4915 Parenthese sequence

    Problem Description bobo found an ancient string. The string contains only three charaters – "( ...

  2. HDU 4915 Parenthese sequence DP

    [题目大意] 题目很短,自己读题吧. [思路] 其实是个很有意思的题. 在括号匹配中,当前的栈存的全是'('.那么,我们可以用dp[i][j]表示到了第i个字符,栈内存j个'('的方法数.这样显然是可 ...

  3. HDU 1005 Number Sequence

    [题目]                                                   Number Sequence Time Limit: 2000/1000 MS (Jav ...

  4. HDU 5400 Arithmetic Sequence

    HDU 5400 Arithmetic Sequence /** HDU 5400 Arithmetic Sequence 直接预处理求解就好了 预处理找出以a[i]结尾最长的subArr长度(满足条 ...

  5. HDU.1005 Number Sequence

    原题 HDU.1005 Number Sequence 分类 杂题 题意 给定一个数列{an}\left\{ a_n \right\}{an​}的前两项a1a_1a1​.a2a_2a2​,以及其递推公 ...

  6. HDU 1560 DNA sequence(DNA序列)

    HDU 1560 DNA sequence(DNA序列) Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K  ...

  7. 校内hu测(10.6T2,T3)(乱搞+贪心+模拟)

    @liu_runda T2.便(then) [题目描述] 给出一个R*C的棋盘.共有R行C列,R*C个格子.现要在每个格子都填一个非负整数.使得任意一个2*2的正方形区域都满足这样的性质:左上角的数字 ...

  8. 【NOIP2013】积木大赛(差分数组,贪心模拟)

    题目 原题链接 问题描述 分析 直观思路--贪心模拟:每次都处理最长正整数区段. 以[2,3,4,1,2][2,3,4,1,2][2,3,4,1,2]为例: [2,3,4,1,2]⟹[1,2,3,0, ...

  9. HDU - 6746 Civilization(贪心+模拟)

    题目链接:点击查看 题目大意:中文题面 题目分析:一道比赛时写崩了的模拟,赛后参考别人的代码,发现原来这个题目可以写的如此简单 说会题目,n 只有 500 ,可以 n * n 枚举每个位置作为起点,对 ...

最新文章

  1. 华为接入鸿蒙,华为将发布鸿蒙平板,魅族宣布接入鸿蒙系统
  2. Spring中ClassPathXmlApplicationContext类的简单使用
  3. 开机后将sim/uim卡上的联系人写入数据库
  4. 硬分叉升级越来越近,BCH社区都在做什么?
  5. 岗位内推 | 微软亚洲互联网工程院自然语言处理组招聘算法研究实习生
  6. Pandas高级操作
  7. selenium webdriver中的常用鼠标操作
  8. MATLAB初学者视频教程
  9. 小白如何开始学习计算机编程?
  10. 使用 reduce 函数计算阶乘
  11. WOW!Illustrator CS6完全自学宝典pdf
  12. 计算机二级考试考的什么内容,计算机二级考试内容考些什么
  13. python一只青蛙一次可以_Python面试题系列之11 变态青蛙跳
  14. CPU处理器Intel Xeon Skylake 6148(2.4 GHz)性能评测
  15. 前端关于自己模拟接口做测试
  16. 为什么RocketMQ是金融核心系统消息中间件的第一选择
  17. 易语言 python库_精易Python支持库 (1.1#1205版)发布啦!
  18. 经过半个月的深入发现CRM-SSM项目如此简单
  19. 麻省理工学院计算机专业怎么样,麻省理工大学的计算机专业怎么样?
  20. Windows系统出现蓝屏怎么办?这些方法可以修复!

热门文章

  1. 区块链研习社北京聚会记录稿
  2. 去除Pycharm中的波浪线
  3. [每天get点新技能]搜商——你不知道的搜索概念:元搜索
  4. python模拟12306官网查询车站车次以及检票口
  5. python + selenium 爬取12306所有车站车次数据
  6. 对2022年TI杯的记录
  7. 光学实验-光电效应(matplotlib)
  8. xbox虚拟服务器,Win8大百科18期:如何在PC上玩Xbox游戏
  9. 欧洲发展最快的金融科技初创公司如何扰乱支付行业
  10. 关于举办第十七届全国大学生智能汽车竞赛的通知