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. 菜鸟版JAVA设计模式-从抽象与实现说桥接模式

    桥接模式,初学的时候事实上非常不理解为什么要把这个模式命名为桥接模式,脑海里突然联想到.事实上我学习是一件比較痛苦的事情,由于我必需要知道来龙去脉才干学的进去,所以,非常快我就对这个命名产生了兴趣,桥 ...

  2. [LeetCode290]Word Pattern

    题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...

  3. Windows Phone 的控件倾斜效果

    原文:Windows Phone 的控件倾斜效果 Windows Phone 7的系统设置里,按钮都有一个点击倾斜的效果,但自己添加的控件就没有.但微软提供了这个效果的代码:TiltEffect MS ...

  4. Linux的proc文件系统

    proc,用户空间和内核空间能够通过该接口通信, 与普通文件不同的是.这些虚拟文件的内容都是动态创建的. proc文件系统是一个伪文件系统,它仅仅存在内存其中,而不占用外存空间. 它以文件系统的方式为 ...

  5. html/css获得第一章

    1.基本教程来学习 大概3天课余时间阅读下面的两个教程. HTML文字教程 CSS文字教程 2.练习 看完教程后.做第一练习时,总结例如以下: 1)div居中 须要设置属性:margin-left:a ...

  6. 【Android基础】eclipse常用快捷键

    Alt+/ 内容辅助键--列出相关内容 Ctrl+shift+o 导入所需的所有类包 Ctrl+1 对现有错误的n中解决方案 Crtl+d 删除所在行或者全部 CTRL+alt+上下键 上下复制 Cr ...

  7. Centos6.5下一个Ceph存储集群结构

    简单的介绍 Ceph的部署模式下主要包括下面几个类型的节点 • Ceph OSDs: A Ceph OSD 进程主要用来存储数据,处理数据的replication,恢复,填充.调整资源组合以及通过检查 ...

  8. Connect2015 简要整理

    2015 简要整理 去年 Connect(); 2014 Visual Studio Contact(); 直播笔记 对于我个人来说,今年 Connect(); 的三个重要发布: ASP.NET 5 ...

  9. [Unity3d]定义自己的鼠标

    [Unity3d]自己定义鼠标 我们在用unity3d开发自己的游戏的时候.自己定义游戏中的鼠标也是常常要用到的.那我就得学学.事实上原理非常easy,先将鼠标给隐藏,然后在鼠标的位置上画出一个自己定 ...

  10. IOS 多于UIImageView 当加载较大的高清闪存管理

    当我们是一家人View  多于UIImageView,和UIImageView表明一个更大的HD,可能存在的存储器的警告的问题.假设第一次走进这个view,无记忆出现预警.当重新进入view,在那曾经 ...