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 (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 172 Accepted Submission(s): 69
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.
A string s1s2…sn (1≤n≤106).
If there is unique valid string, print "Unique". If there are no valid strings at all, print "None". Otherwise, print "Many".
?? ??? ?
(? ?
Unique
Many
None
pid=4915" style="color:rgb(26,92,200); text-decoration:none">Statistic pid=4915" style="color:rgb(26,92,200); text-decoration:none">Submit 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的更多相关文章
- hdu 4915 Parenthese sequence(模拟)2014多培训学校5现场
Parenthese sequence Time Limit: ...
- HDU 4915 Parenthese sequence
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4915 解题报告:从前往后遍历一次,每次判断')'的数目是不是满足 n < (i +1)/ 2,从 ...
- HDU 4915 Parenthese sequence _(:зゝ∠)_ 哈哈
哦,我没做 #include <cstdio> #include <cstring> #include <algorithm> const int N = 1000 ...
- hdu 4915 Parenthese sequence 多校第五场
推断一个序列是否是有效是简单的. 可是推断序列是不是有多个解会出问题. 那么从i=0 ~l 假设读到问号,推断该问号成为(能否有效,该问号为)是否有效. 假设都有效,则必有多个解. 假设都无效,则无解 ...
- HDU 6091 - Rikka with Match | 2017 Multi-University Training Contest 5
思路来自 某FXXL 不过复杂度咋算的.. /* HDU 6091 - Rikka with Match [ 树形DP ] | 2017 Multi-University Training Conte ...
- HDU 6125 - Free from square | 2017 Multi-University Training Contest 7
思路来自这里 - - /* HDU 6125 - Free from square [ 分组,状压,DP ] | 2017 Multi-University Training Contest 7 题意 ...
- HDU 6129 - Just do it | 2017 Multi-University Training Contest 7
比赛时脑子一直想着按位卷积... 按题解的思路: /* HDU 6129 - Just do it [ 规律,组合数 ] | 2017 Multi-University Training Contes ...
- HDU 6088 - Rikka with Rock-paper-scissors | 2017 Multi-University Training Contest 5
思路和任意模数FFT模板都来自 这里 看了一晚上那篇<再探快速傅里叶变换>还是懵得不行,可能水平还没到- - 只能先存个模板了,这题单模数NTT跑了5.9s,没敢写三模数NTT,可能姿势太 ...
- HDU 6093 - Rikka with Number | 2017 Multi-University Training Contest 5
JAVA+大数搞了一遍- - 不是很麻烦- - /* HDU 6093 - Rikka with Number [ 进制转换,康托展开,大数 ] | 2017 Multi-University Tra ...
随机推荐
- 使用 WPF 实现所见即所得HTML编辑器
Introduction In this tip, you will learn the use of WPF webbrowser control and the use of the librar ...
- redmine使用汇总redmine软件工程过程
1.强制性当一个新的问题:轨道.议题.叙述性说明.状态.优先.分配给.父任务(假设没有可以不填,假设有请务必填写).开始日期.预计时间.党羽,请注意,这时候一定不要填写完成时间 2.如果你想在实时登记 ...
- 管理Android通信录
Android提供了Contacts应用程序来管理联系人,并且Android系统还为联系人管理提供了ContentProvider,这就同意其他应用程序以ContentResolver来管理联系人数据 ...
- 冒泡排序----java实现
冒泡排序思路:第1次:顺序比较从第0个到第len个(相邻两个)元素并把大的放后面,第一次进行完后,最大 的元素会在最后: ...
- lunux命令笔记
文件查看命令 ls / -lh ls list / 路径 -l 具体 -lh 具体的人性化显示 -ld 显示文件夹 -i 显示i节点 mkdir /tmp/mulu/mulu2 /tmp/ma/mb ...
- 采用objdump调试驱动程序
最近的一个推断调整nand是好是坏司机+测试程序,因此,与下面的调整过程.看来他也学到了一点知识.因此,关于备案. 这篇文章主要是讲述调式驱动的一个方法而已. 先来看看測试程序 #include &l ...
- Winform WebBrowser引用IE版本问题
做了一个Winform的项目.项目里使用了WebBrowser控件.以前一直都以为WebBrowser是直接调用的系统自带的IE,IE是呈现出什么样的页面WebBrowser就呈现出什么样的页面.其实 ...
- javascript的位操作、整数、二进制
位与(x&y):对操作数进行二进制与的操作,如果两个操作数的某一位两个都为1,将对应的结果位设为1. 0x0007 & 0x0003 = 0x0003 \ 一个小型年老棕色的狗:64 ...
- 2014年度辛星全然解读html第七节
经过前面六节的学习,我们大致清楚了HTML教程中的基础内容,那么接下来我们開始继续向后推进,能够说,以下我们介绍一下HTML中的区块. ***************区块*************** ...
- 学习vi和vim编辑(3):一个简单的文本编辑器(2)
然后文章,继续评论vi编辑简单的文本编辑命令. 本文主要是删除的文字.复制,运动命令. 删除文本: 正如上一篇文章中讲过的,对于删除命令("d")也具有"(command ...