主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4915

Parenthese sequence

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

Total Submission(s): 172    Accepted Submission(s): 69

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
 
Author
Xiaoxu Guo (ftiasch)
 
Source
 
Recommend
We have carefully selected several similar problems for you:  4919 4918 4917 4916 4914 
 

pid=4915" style="color:rgb(26,92,200); text-decoration:none">Statistic | 

pid=4915" style="color:rgb(26,92,200); text-decoration:none">Submit | Discuss | 

pid=4915" style="color:rgb(26,92,200); text-decoration:none">Note

这道题目,比赛的时候和队友讨论了一下,认为搜索TLE的可能性巨大。于是果断採取了其它的方法。

我们的方法事实上就是扫了二遍。中复杂度接近O(N).

用cnt来统计‘(’的数量。接着模拟一个链表来存储可能发生变化的'?'.

head表示链表头。遇到')'时若cnt>0。即前面还有'('剩余时,则让cnt--,即相互抵消掉。若cnt==0,,则取

出链表中的一个元素,把它改成'(',即让cnt++。到最后假设cnt还有剩,则显然是不可能的,但要推断是"Unique"

还是"Many"。则须要把表头所表示的元素改成'(',若还是符合则是"Many" 否则是"Unique".

详见程序啦。

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<map>
#include<set>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<bitset>
using namespace std;
#define CLR(A) memset(A,-1,sizeof(A))
typedef long long ll;
const int MAX=1000010;
char str[MAX];
int next[MAX],head=-1,cnt,tail=0;
int solve(){
head=-1;cnt=0;tail=0;
CLR(next);
for(int i=0;str[i];i++){
if(str[i]=='(') cnt++;
else if(str[i]==')'){
if(cnt==0){
if(head==-1) return 0;
cnt++;
head=next[head];
}
else cnt--;
}
else{
if(cnt>0){
cnt--;
next[tail]=i;
tail=i;
if(head==-1) head=i;
}
else{
cnt++;
}
}
}
if(cnt!=0) return 0;
else return 1;
}
int main(){
while(~scanf("%s",str)){
int len=strlen(str);
bool ret=1;
if(len&1){
printf("None\n");continue;
}
ret=solve();
if(ret==0){
printf("None\n");continue;
}
if(head==-1){
printf("Unique\n");continue;
}
str[head]='(';
ret=solve();
if(ret==0) printf("Unique\n");
else printf("Many\n");
}
return 0;
}

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

hdu 4915 Parenthese sequence--2014 Multi-University Training Contest 5的更多相关文章

  1. hdu 4915 Parenthese sequence(模拟)2014多培训学校5现场

    Parenthese sequence                                                                     Time Limit: ...

  2. HDU 4915 Parenthese sequence

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

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

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

  4. hdu 4915 Parenthese sequence 多校第五场

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

  5. HDU 6091 - Rikka with Match | 2017 Multi-University Training Contest 5

    思路来自 某FXXL 不过复杂度咋算的.. /* HDU 6091 - Rikka with Match [ 树形DP ] | 2017 Multi-University Training Conte ...

  6. HDU 6125 - Free from square | 2017 Multi-University Training Contest 7

    思路来自这里 - - /* HDU 6125 - Free from square [ 分组,状压,DP ] | 2017 Multi-University Training Contest 7 题意 ...

  7. HDU 6129 - Just do it | 2017 Multi-University Training Contest 7

    比赛时脑子一直想着按位卷积... 按题解的思路: /* HDU 6129 - Just do it [ 规律,组合数 ] | 2017 Multi-University Training Contes ...

  8. HDU 6088 - Rikka with Rock-paper-scissors | 2017 Multi-University Training Contest 5

    思路和任意模数FFT模板都来自 这里 看了一晚上那篇<再探快速傅里叶变换>还是懵得不行,可能水平还没到- - 只能先存个模板了,这题单模数NTT跑了5.9s,没敢写三模数NTT,可能姿势太 ...

  9. HDU 6093 - Rikka with Number | 2017 Multi-University Training Contest 5

    JAVA+大数搞了一遍- - 不是很麻烦- - /* HDU 6093 - Rikka with Number [ 进制转换,康托展开,大数 ] | 2017 Multi-University Tra ...

随机推荐

  1. 在WPF中处理Windows消息

    在Winform中 处理Windows消息通过重写WndProc方法 在WPF中 使用的是System.Windows. Sytem.Windows.Controls等名字空间,没有WndProc函数 ...

  2. 高速理解掌握node.js 字符编码,确码过程 以及base64编解码原理

    var buf1 = new Buffer('a','ascii'); // 字符'a' -> ascii编码 -> 61 存在计算机中 console.log(buf1); var b ...

  3. HTML5游戏开发实战--当心

    1.WebSocket它是HTML5该标准的一部分.Web页面可以用它来连接到持久socketserver在.该接口提供一个浏览器和server与事件驱动的连接.这意味着client每次需要时不再se ...

  4. 举例说, Decorator模式(Decorator Pattern)

    前言    在食品工业中的装饰图案具有比较广泛的应用,大多数的两个图案和在网上的例子饮食相关的,一旦被称为电影的手表,点咖啡要加糖要加奶昔要加这加那的时候.感觉好有派~好高大上啊~.为啥我在小卖部都是 ...

  5. 并行编程条件变量(posix condition variables)

    在整理Java LockSupport.park()东方的,我看到了"Spurious wakeup",通过重新梳理. 首先,可以在<UNIX级别编程环境>在样本: # ...

  6. PHP如何添加内置的扩展

    什么时候server上PHP已安装.需要额外补充PHP如何扩展?你并不需要再次安装PHP.同phpize我们可以在原PHP安装扩展直接的基础上,. 这次编译只不过单独编译PHP的扩展库.接下来将编译好 ...

  7. 【最小割】HDU 4971 A simple brute force problem.

    说是最大权闭合图.... 比赛时没敢写.... 题意 一共同拥有n个任务,m个技术 完毕一个任务可盈利一些钱,学习一个技术要花费钱 完毕某个任务前须要先学习某几个技术 可是可能在学习一个任务前须要学习 ...

  8. Golang+Mongodb

    Golang+Mongodb打造你的第一个站点 很多人推荐MEAN来开发站点.MEAN就是M:mongodb,E:expressjs.A:angular最后的N:nodejs. 但是如果你亲身的体会到 ...

  9. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  10. 如果不能显示真正的考验个别车型toast问题解决

    当真正的考验个别车型toast不显示信息,找到两个解决方式.不知还有什么其他有效方法.期待大神们的分享!.! 1.手动方案:设置-->应用软件管理-->相应APP-->勾选显示通知框 ...