hdu 4915 Parenthese sequence(模拟)2014多培训学校5现场
Parenthese sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072
K (Java/Others)
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
’既能够当做’(‘, 又能够当做’)‘,求有多少种方法满足括号匹配。假设不能匹配,输出“None”;假设仅仅有一种,输出“Unique”;否则输出“Many”。
’,我们能够先把这个‘?’变成‘(’,推断是否匹配。再把‘?
’变成')',推断是否匹配。
#include<cstdio>
#include<cstring>
const int N = 1e6 + 50;
char str[N], s[N];
int len;
int judge() //推断当前的字符串是否匹配
{
int l = 0; //记录左括号的数量
int r = 0; //记录右括号的数量
int num = 0; //记录已经遍历过的字符数量
int i;
for(i = 0; i < len; i++) //从前往后推断
{
num++;
if(num == 1)
{
if(s[i] == '?')
s[i] = '(';
}
if(s[i] == '(') l++;
else if(s[i] == ')') r++;
if(r > num/2) //右括号数量太多。无法全然匹配
return 0;
if(r * 2 == num) //前num个能够全然匹配
{
l = r = num = 0;
}
}
if(l > num/2) return 0; num = l = r = 0;
for(i = len - 1; i >= 0; i--) //从后往前推断
{
num++;
if(num == 1)
{
if(s[i] == '? ')
s[i] = ')';
}
if(s[i] == '(') l++;
else if(s[i] == ')') r++;
if(l > num / 2) return 0; //左括号数量太多,无法全然匹配
if(l * 2 == num) //后num个能够全然匹配
{
l = r = num = 0;
}
}
if(r > num/2) return 0; return 1;
}
int main()
{
int flag_l, flag_r, i;
while(~scanf("%s",str))
{
len = strlen(str);
if(len & 1)
{
printf("None\n");
continue;
} strcpy(s, str);
flag_l = judge(); //如果没有 '? ',推断是否匹配 if(!flag_l)
{
printf("None\n");
continue;
}
for(i = 0; i < len; i++)
{
if(str[i] == '?')
{
strcpy(s, str); s[i] = ')';
flag_l = judge(); s[i] = '(';
flag_r = judge(); if(flag_l && flag_r)
{
printf("Many\n");
break;
}
if(!flag_l && !flag_r)
{
printf("None\n");
break;
}
if(flag_l && !flag_r)
s[i] = ')';
else if(!flag_l && flag_r)
s[i] = '(';
}
}
if(i == len)
printf("Unique\n");
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
hdu 4915 Parenthese sequence(模拟)2014多培训学校5现场的更多相关文章
- 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 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场
Matrix multiplication Time ...
- 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 4862 Jump(更多的联合培训学校1)(最小费用最大流)
职务地址:pid=4862">HDU4862 最小费用流做的还是太少. 建图想不出来. . . 直接引用官方题解的话吧... 最小K路径覆盖的模型.用费用流或者KM算法解决,构造二部图 ...
- hdu4915 Parenthese sequence 贪心O(n)解法(new)
hdu4915 Parenthese sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- HDU 5860 Death Sequence(死亡序列)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
随机推荐
- [Ruby]How to create singleton class ?
Singleton is one design pattern in the software engineering. Ruby has its own special feature to dec ...
- java提高篇(四)-----抽象类与接口
接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是java语言中对抽象概念进行定义的两种机制,正是由于他们的存在才赋予java强大的面向对象的能力.他们两者之间对抽象概念 ...
- POJ 1915 Knight Moves(BFS+STL)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20913 Accepted: 9702 ...
- Android利用网络编程HttpClient批量上传(一个)
请尊重他人的劳动成果.转载请注明出处:Android网络编程之使用HttpClient批量上传文件 我曾在<Android网络编程之使用HTTP訪问网络资源>一文中介绍过HttpCient ...
- configure.ac:20: error: Autoconf version 2.65 or higher is required
安装thrift例如,下面的问题出现: configure.ac:20: error: Autoconf version 2.65 or higher is required wget http:// ...
- ef添加字段
先在实体类里添加字段 ,然后执行 Add-Migration updateNumberOfLikes Update-Database -Verbose
- Spring FrameWork4(MVC + IOC)高速入门实例
使用Maven创建project并配置依赖项 首先创建一个Maven Project: 然后选择创建Maven 的webapp实例,当然也能够通过命令行方式创建Maven webapp的项目再转化并导 ...
- form 为什么上传文件enctype现场
FORM要素enctype属性指定表单数据server当提交所使用的编码类型,默认默认值它是"application/x-www-form-urlencoded". 这样的编码方式 ...
- 我有DIY一Android遥控-所有开源
我有DIY一Android遥控-所有开源 1.试用 记得宋宝华在「设备驱动开发具体解释」提出一个这种理论「软件和硬件互相渗透对方的领地」,这次证明还是确实是这样,使用上层APP软件加上简单的更为简单的 ...
- HDU 2454 Degree Sequence of Graph G(Havel定理 推断一个简单图的存在)
主题链接:pid=2454">http://acm.hdu.edu.cn/showproblem.php?pid=2454 Problem Description Wang Haiya ...