Parenthese sequence

                                                                    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072
K (Java/Others)

Problem Description
bobo found an ancient string. The string contains only three charaters -- "(", ")" and "?".



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.

 
Input
The input consists of several tests. For each tests:



A string s1s2…sn (1≤n≤106).
 
Output
For each tests:



If there is unique valid string, print "Unique". If there are no valid strings at all, print "None". Otherwise, print "Many".
 
Sample Input
??
? ???
(??
 
Sample Output
Unique
Many
None
 
题意:输入一个长度不超过10^6的字符串,串中仅仅包括‘(’、‘)’、‘?’。当中‘?

’既能够当做’(‘, 又能够当做’)‘,求有多少种方法满足括号匹配。假设不能匹配,输出“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现场的更多相关文章

  1. HDU 4915 Parenthese sequence

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4915 解题报告:从前往后遍历一次,每次判断')'的数目是不是满足 n < (i +1)/ 2,从 ...

  2. HDU 4915 Parenthese sequence _(:зゝ∠)_ 哈哈

    哦,我没做 #include <cstdio> #include <cstring> #include <algorithm> const int N = 1000 ...

  3. hdu 4915 Parenthese sequence 多校第五场

    推断一个序列是否是有效是简单的. 可是推断序列是不是有多个解会出问题. 那么从i=0 ~l 假设读到问号,推断该问号成为(能否有效,该问号为)是否有效. 假设都有效,则必有多个解. 假设都无效,则无解 ...

  4. hdu 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场

    Matrix multiplication                                                                           Time ...

  5. 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 ...

  6. HDU 4862 Jump(更多的联合培训学校1)(最小费用最大流)

    职务地址:pid=4862">HDU4862 最小费用流做的还是太少. 建图想不出来. . . 直接引用官方题解的话吧... 最小K路径覆盖的模型.用费用流或者KM算法解决,构造二部图 ...

  7. hdu4915 Parenthese sequence 贪心O(n)解法(new)

    hdu4915 Parenthese sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...

  8. HDU 5860 Death Sequence(死亡序列)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  9. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

随机推荐

  1. 新秀系列C/C++经典问题(六)

    类包含一个指向成员复制 称号:下面是类和执行的阵列的声明.题.并针对存在的问题提出几种解决方式. template<typename T> class Array { public: Ar ...

  2. 使用 node-inspector 调试 Node.js

    大部分基于 Node.js 的应用都是执行在浏览器中的, 比如强大的调试工具 node-inspector. node-inspector 是一个全然基于 Node.js 的开源在线调试工具,提供了强 ...

  3. spring集成quartz

    spring集成quartz 注意:出现异常"Caused by: java.lang.IncompatibleClassChangeError: class org.springframe ...

  4. javascript实现函数的默认參数值方法

    近期在学python,得益于python中的decorator思路,想到在javascript中參数是不能定义默认值的,可是能够通过decorator给它模拟出来,话不多说,上代码 <!DO ...

  5. 【Java收集的源代码分析】Hashtable源代码分析

    Hashtable简单介绍 Hashtable相同是基于哈希表实现的,相同每一个元素是一个key-value对,其内部也是通过单链表解决冲突问题,容量不足(超过了阀值)时.相同会自己主动增长. Has ...

  6. therefore/so/hence/then/accordingly/Thus

    这几个词的区别大致可从以下几方面去看:1.therefore adv.因此, 所以=for that reason=consequently常用于连接两个并列分句,其前加“and”或分号“:”.He ...

  7. War文件部署(转)

    其实,开始要求将源码压缩成War文件时,一头雾水! 公司项目要求做CAS SSO单点登录 也就是这玩意.... 其实war文件就是Java中web应用程序的打包.借用一个老兄的话,“当你一个web应用 ...

  8. 恶意软件&quot;跨平台&quot; 小心钱包很受伤

    什么是跨平台攻击? 举例来说.就像网络诈骗犯为了避开电子商务平台的监控.会在微博上发消息.百度上撒网,腾讯上联系,最后在淘宝上交易.这样的跨平台操作的模式会大大添加犯罪过程监控和取证的难度.而跨平台攻 ...

  9. rest服务器

    一个轻量级rest服务器   RestServer直接发布数据库为json格式提供方法 RestSerRestServer直接发布数据库为json格式 支持MySQL,SqlServer,Oracle ...

  10. javascript滚动栏响应鼠标滑轮的实现上下滚动事件

    实现鼠标滚动滚轮事件: <script type="text/javascript"><pre name="code" class=" ...