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 ...
随机推荐
- org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback.....Parameter index out of range (1 > number of parameters, which is 0).;
sql有误,一般是 sql语句少了问号.
- windows API 开发飞机订票系统 图形化界面 (二)
首先,用到的数据结构的定义.以及全局变量和函数的声明如下: // Flight.c : 定义应用程序的入口点. // #include "stdafx.h" //订单 typede ...
- Linq之常见关键字
目录 写在前面 系列文章 常见关键字 总结 写在前面 前面的几篇文章算是对linq的铺垫,从本篇开始将进行linq的语法及实践. 系列文章 Linq之Lambda表达式初步认识 Linq之Lambda ...
- java通过地址获取主机名
关键代码: try { String str=Chat.getJt().getText().toString();//获取输入内容 String[] ipstr=str.split("[.] ...
- 关于软工项目beta版本
项目总结 项目成员: 黄丰润 031302307 王旭銮 031302320 张家俊 031302329 张晓燕 031302343 项目完成度:实现了专业信息填写.查看,教师信息填写,报课和查看课表 ...
- Spring security 和 AOP 学习
1.Spring security 登录验证拦截器 资源管理拦截器 认证和授权: 认证:登录时候确实存在此用户. 登录要认证! 授权:登录后判断权限级别,然后赋予相应的操作权限. ...
- 使用Owin中间件搭建OAuth2.0认证授权服务器
前言 这里主要总结下本人最近半个月关于搭建OAuth2.0服务器工作的经验.至于为何需要OAuth2.0.为何是Owin.什么是Owin等问题,不再赘述.我假定读者是使用Asp.Net,并需要搭建OA ...
- visual studio各个版本的差异
- BZOJ-1927 星际竞速 最小费用最大流+拆点+不坑建图
1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MB Submit: 1593 Solved: 967 [Submit][Statu ...
- 利用Spring中的HtmlUtils.htmlEscape(input)过滤html
fatherModule.setModuelName(HtmlUtils.htmlEscape(fatherModule.getModuelName())); log.info(HtmlUtils.h ...