HDU 4915 Parenthese sequence
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4915
解题报告:从前往后遍历一次,每次判断')'的数目是不是满足 n < (i +1)/ 2,从后往前遍历一次,每次判断'('的数目是不是满足 n <= (len - i) / 2;
这样就可以判断出这个序列是否存在匹配的序列。接下来就是判断是Many还是Unique的情况,因为数据是10的六次方,所以遇到问号改成'(' 或')'暴力判断是不行的,但是我们可以判断出只要(和)的数目小于等于len / 2而且有三个连续的?那句是Many,否则再进行暴力判断,这样就可以大大减小时间复杂度。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = +;
char str[maxn]; int judge(char* str)
{
int len = strlen(str);
int a = ,b = ;
for(int i = ,j = len- ;i < len,j >= ;++i,--j)
{
if(str[i] == ')') a++;
if(str[j] == '(') b++;
if(a > (i + ) / ) return ;
if(b > (len - j) / ) return ;
}
return ;
}
int main()
{
// freopen("1005.in","r",stdin);
int T = ;
while(scanf("%s",str)!=EOF)
{
T++;
int ans;
ans = judge(str);
int l = strlen(str);
if(l & ) ans = ;
if(!ans)
{
puts("None");
continue;
}
int len = strlen(str);
int a = ,b = ,c = ,f = ,M = ;
for(int i = ;i < len;++i)
{
if(str[i] == '(') a++;
if(str[i] == ')') b++;
if(str[i] == '?')
{
f++;
c++;
}
else
{
M = max(M,f);
f = ;
}
}
if(a >= len / || b >= len / || c <= )
{
puts("Unique");
continue;
}
if(M >= )
{
puts("Many");
continue;
}
ans = ;
for(int i = ;i < len;++i)
if(str[i] == '?')
{
int tt = ;
str[i] = '(';
tt += judge(str);
str[i] = ')';
tt += judge(str);
if(tt == )
{
ans = ;
break;
}
str[i] = '?';
}
printf(ans? "Many\n":"Unique\n");
}
return ;
}
HDU 4915 Parenthese sequence的更多相关文章
- hdu 4915 Parenthese sequence(模拟)2014多培训学校5现场
Parenthese sequence Time Limit: ...
- HDU 4915 Parenthese sequence _(:зゝ∠)_ 哈哈
哦,我没做 #include <cstdio> #include <cstring> #include <algorithm> const int N = 1000 ...
- hdu 4915 Parenthese sequence 多校第五场
推断一个序列是否是有效是简单的. 可是推断序列是不是有多个解会出问题. 那么从i=0 ~l 假设读到问号,推断该问号成为(能否有效,该问号为)是否有效. 假设都有效,则必有多个解. 假设都无效,则无解 ...
- 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 ...
- HDU 5860 Death Sequence(死亡序列)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu4915 Parenthese sequence 贪心O(n)解法(new)
hdu4915 Parenthese sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 5860 Death Sequence(递推)
HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...
随机推荐
- 定制类自己的的new_handler
C++中的new操作符首先使用operator new函数来分配空间,然后再在此空间上调用类的构造函数构造对象.当operator new无法分配所需的内存空间时,默认的情况下会抛出一个bad_all ...
- 《1024伐木累》-te别篇,庭审你知道吗?
思前想后,我觉得不应该发这一期,因为,做完这一期之后突然发觉,自己失去了主题,到底是在讽刺?还是在讽刺?还是在讽刺呢?不论是什么,大家自己判断吧.就当作者不想发表自己的观点,先看这一期的对白吧! 1. ...
- C头文件之<stdio.h>
(stdio.h) 该头文件主要是执行输入输出操作.文件中重要的概念是“流”(streams).“流”在函数库中用FILE表示,用指针类型FILE *来操作.有三个标准流:stdin, stdout, ...
- [转]window.opener用法
window.opener 实际上就是通过window.open打开的窗体的父窗体. 比如在父窗体parentForm里面 通过 window.open("subForm.html" ...
- 【bzoj1005】 HNOI2008—明明的烦恼
http://www.lydsy.com/JudgeOnline/problem.php?id=1005 (题目链接) 题意 给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多 ...
- jquery中的prop和attr比较区别
近期和一同事争执prop和attr的区别,也查了很多,同事说它只是特性和固有属性的区别,但是我也查到了一些其他的,故此,来总结一下吧! 1.固有属性和特别属性 对于HTML元素本身就带有的固有属性,在 ...
- 洛谷P2320 [HNOI2006]鬼谷子的钱袋
https://www.luogu.org/problem/show?pid=2320#sub 题目描述全是图 数学思维,分治思想 假设总数为n 从n/2+1到n的数都可以用1~n的数+n/2表示出来 ...
- C/C++代码中的笔误
1. 在printf()的参数前加& (2015/10/7) 这是我写的一个数据生成器(generator)片段 +; printf("%d\n", &n);
- git分支与版本管理、版本回退、冲突解决记录
一.基础使用 1.初始化本地仓库 git init 2.关联远程仓库 git remote add origin git@github.com:用户名/仓库名.git 3.添加远程仓库文件到本地 gi ...
- c3p0配置详解
数据库连接池C3P0框架是个非常优异的开源jar,高性能的管理着数据源,这里只讨论程序本身负责数据源,不讨论容器管理. 一.实现方式: C3P0有三种方式实现: 1.自己动手写代码,实现数据源 例如: ...