Parenthese sequence

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

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
 
题意:输入一个长度不超过10^6的字符串,串中仅仅包括‘(’、‘)’、‘?’。当中‘?

’既能够当做’(‘, 又能够当做’)‘,求有多少种方法满足括号匹配。假设不能匹配,输出“None”;假设仅仅有一种,输出“Unique”;否则输出“Many”。

分析:假设没有‘?’。问题就变成了简单的括号匹配。这个非常好推断;
每次遇到一个‘?

’,我们能够先把这个‘?’变成‘(’,推断是否匹配。再把‘?

’变成')',推断是否匹配。

假设都匹配,则有多种匹配方法。
假设都不匹配,则无法匹配;
假设仅仅有一个匹配,则把这个‘?’变成使之匹配的括号,然后继续推断下一个‘?'就可以。
#include<cstdio>
#include<cstring>
const int N = 1e6 + 50;
char str[N], s[N];
int len;
int judge() //推断当前的字符串是否匹配
{
int l = 0; //记录左括号的数量
int r = 0; //记录右括号的数量
int num = 0; //记录已经遍历过的字符数量
int i;
for(i = 0; i < len; i++) //从前往后推断
{
num++;
if(num == 1)
{
if(s[i] == '?')
s[i] = '(';
}
if(s[i] == '(') l++;
else if(s[i] == ')') r++;
if(r > num/2) //右括号数量太多。无法全然匹配
return 0;
if(r * 2 == num) //前num个能够全然匹配
{
l = r = num = 0;
}
}
if(l > num/2) return 0; num = l = r = 0;
for(i = len - 1; i >= 0; i--) //从后往前推断
{
num++;
if(num == 1)
{
if(s[i] == '? ')
s[i] = ')';
}
if(s[i] == '(') l++;
else if(s[i] == ')') r++;
if(l > num / 2) return 0; //左括号数量太多,无法全然匹配
if(l * 2 == num) //后num个能够全然匹配
{
l = r = num = 0;
}
}
if(r > num/2) return 0; return 1;
}
int main()
{
int flag_l, flag_r, i;
while(~scanf("%s",str))
{
len = strlen(str);
if(len & 1)
{
printf("None\n");
continue;
} strcpy(s, str);
flag_l = judge(); //如果没有 '? ',推断是否匹配 if(!flag_l)
{
printf("None\n");
continue;
}
for(i = 0; i < len; i++)
{
if(str[i] == '?')
{
strcpy(s, str); s[i] = ')';
flag_l = judge(); s[i] = '(';
flag_r = judge(); if(flag_l && flag_r)
{
printf("Many\n");
break;
}
if(!flag_l && !flag_r)
{
printf("None\n");
break;
}
if(flag_l && !flag_r)
s[i] = ')';
else if(!flag_l && flag_r)
s[i] = '(';
}
}
if(i == len)
printf("Unique\n");
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

hdu 4915 Parenthese sequence(模拟)2014多培训学校5现场的更多相关文章

  1. HDU 4915 Parenthese sequence

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4915 解题报告:从前往后遍历一次,每次判断')'的数目是不是满足 n < (i +1)/ 2,从 ...

  2. HDU 4915 Parenthese sequence _(:зゝ∠)_ 哈哈

    哦,我没做 #include <cstdio> #include <cstring> #include <algorithm> const int N = 1000 ...

  3. hdu 4915 Parenthese sequence 多校第五场

    推断一个序列是否是有效是简单的. 可是推断序列是不是有多个解会出问题. 那么从i=0 ~l 假设读到问号,推断该问号成为(能否有效,该问号为)是否有效. 假设都有效,则必有多个解. 假设都无效,则无解 ...

  4. hdu 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场

    Matrix multiplication                                                                           Time ...

  5. hdu 4915 Parenthese sequence--2014 Multi-University Training Contest 5

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4915 Parenthese sequence Time Limit: 2000/1000 MS (Ja ...

  6. HDU 4862 Jump(更多的联合培训学校1)(最小费用最大流)

    职务地址:pid=4862">HDU4862 最小费用流做的还是太少. 建图想不出来. . . 直接引用官方题解的话吧... 最小K路径覆盖的模型.用费用流或者KM算法解决,构造二部图 ...

  7. hdu4915 Parenthese sequence 贪心O(n)解法(new)

    hdu4915 Parenthese sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...

  8. HDU 5860 Death Sequence(死亡序列)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  9. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

随机推荐

  1. 得到View Frustum的6飞机

    笔者:i_dovelemon 资源:CSDN 日期:2014 / 9 / 30 主题:View Frustum, Plane, View Matrix, Perspective Projection ...

  2. 基于Spring + Spring MVC + Mybatis 高性能web构建

    基于Spring + Spring MVC + Mybatis 高性能web构建 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJs,做了大量的研究,对前后端交互 ...

  3. java提高篇(十一)-----代码块

    在编程过程中我们可能会遇到如下这种形式的程序: public class Test { { //// } } 这种形式的程序段我们将其称之为代码块,所谓代码块就是用大括号({})将多行代码封装在一起, ...

  4. hbase开放lzo压缩

    hbase仅仅支持对gzip的压缩,对lzo压缩支持不好. 在io成为系统瓶颈的情况下,一般开启lzo压缩会提高系统的吞吐量. 但这须要參考详细的应用场景,即是否值得进行压缩.压缩率是否足够等等.  ...

  5. 开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式

    原文:[原创]开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式 开源Math.NET基础数学类库使用系列文章总目录:   1.开源.NET基础数学计算组件Math. ...

  6. 使用axis公布weblogic(一个)

    1.在MyEclipse创建一个新的Web Project.新类,声明函数名称.参数,为了不写功能实体. 2.将java2wsdl.bat将文件复制到classes目录,据内部参数更改自己的实际情况 ...

  7. POJ 1984 Navigation Nightmare (数据结构-并检查集合)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4072   Accepted: 1 ...

  8. java提高篇(十三)-----字符串

          可以证明,字符串操作是计算机程序设计中最常见的行为. 一.String 首先我们要明确,String并不是基本数据类型,而是一个对象,并且是不可变的对象.查看源码就会发现String类为f ...

  9. 物理卷操作命令:pvcreate,pvscan,pvdisplay.卷组操作命令:vgcreate,vgdisplay. (转)

    新硬盘创建LVM系统过程. 物理卷操作命令:pvcreate,pvscan,pvdisplay. 卷组操作命令:vgcreate,vgdisplay. 逻辑卷操作命令:lvcreate,lvdispl ...

  10. android adb 不同的方式使用特定的解释

    本文介绍windows 在程序中使用adb 方法.没有引进adb 该命令. 1) 启动adb 流程.获得输出从管道. 这样的方式的弊端有多少,我也不知道.反正就是各种问题吧.可是眼下我问过非常多朋友. ...