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 ...
随机推荐
- webstorm调试Node的时候配置
点击Edit Configurations的这个的配置:(不能点击是因为目前你选中的不是项目)
- 【BZOJ1006】【HNOI2008】神奇的国度(弦图染色)
1006: [HNOI2008]神奇的国度 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1467 Solved: 603[Submit][Stat ...
- 每天一个linux命令(36):top命令
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是 一个动态显示过程,即可以通过用户按键来不断刷 ...
- NABCD模型进行竞争性需求分析
确定项目:教室管理系统 负责人:李凤娇,高德建 选择比努力更重要.一个项目成功自然离不开组员们的努力.但是,光努力是不够的.还需要用户有需求,能快速实现. 这些东西,看似很虚,却能让我们少走不少弯路. ...
- xcode7 __weak 导致报错 is unavailable
Build Settings Apple LLVM7.1 Laguage-Object-c Weak References in Manual Retain Release 选 Yes;
- js获取服务器时间戳
<!DOCTYPE html> <html> <head> <title>ajax</title> </head> <bo ...
- hdu1828 线段树+离散化+扫描线
添加lb[],rb[]数组,来标记竖边.添加num,来计算竖边的个数,因为计算周长的时候,未覆盖的竖边都要加. #include<stdio.h> #include<stdlib.h ...
- FastDFS在centos上的安装配置与使用
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线服务.(百 ...
- Dancing Links初学记
记得原来备战OI的时候,WCX大神就研究过Dancing Links算法并写了一篇blog.后来我还写了个搜索策略的小文章( http://www.cnblogs.com/pdev/p/3952279 ...
- FatMouse的交易问题
想按照某个值排序,用sort()函数,结果想了半天不知道用数组怎么解决,然后看了答案,才知道原来可以用struct,想想我真是笨死了.. 原题描述以及答案如下: Problem Description ...