hdu 4915 Parenthese sequence(贪心,模拟)
来源:互联网 发布:noob厂 知乎 编辑:程序博客网 时间:2024/05/16 09:33
题目:
Parenthese sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1181 Accepted Submission(s): 485
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.
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).
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".
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
UniqueManyNone
Author
Xiaoxu Guo (ftiasch)
Source
2014 Multi-University Training Contest 5
题目:给一个字符串,只包含左括号,右括号和问号,问号可以当左括号或右括号来使用,问你这能不能形成一个合法的括号序列,如果可以的话是唯一解还是多解。
思路:我们可以从左向右扫一遍再从右向左扫一遍。从左到右的时候维护出现的右括号的数目r,如果r>扫过的数目/2,说明肯定不能形成合法的序列,如果r*2=扫过的数目,那么扫过的部分形成了一个合法的序列,就将r和扫过的数目清空。扫完后看是否左括号的数目大于扫过的数目/2,从右往左扫也是类似的。如果从左往右扫和从右往左扫都合法,就枚举每个问号,分别试试它作为左括号的情况和右括号的情况,只有一种情况合法的时候把那个位置的问号换成相应的值,两个位置可以就是多解,都不可以的话就是无解。
代码:
#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include<climits>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;#define PB push_back#define MP make_pair#define REP(i,x,n) for(int i=x;i<(n);++i)#define FOR(i,l,h) for(int i=(l);i<=(h);++i)#define FORD(i,h,l) for(int i=(h);i>=(l);--i)#define SZ(X) ((int)(X).size())#define ALL(X) (X).begin(), (X).end()#define RI(X) scanf("%d", &(X))#define RII(X, Y) scanf("%d%d", &(X), &(Y))#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))#define DRI(X) int (X); scanf("%d", &X)#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)#define OI(X) printf("%d",X);#define RS(X) scanf("%s", (X))#define MS0(X) memset((X), 0, sizeof((X)))#define MS1(X) memset((X), -1, sizeof((X)))#define LEN(X) strlen(X)#define F first#define S second#define Swap(a, b) (a ^= b, b ^= a, a ^= b)#define Dpoint strcut node{int x,y}#define cmpd int cmp(const int &a,const int &b){return a>b;} /*#ifdef HOME freopen("in.txt","r",stdin); #endif*/const int MOD = 1e9+7;typedef vector<int> VI;typedef vector<string> VS;typedef vector<double> VD;typedef long long LL;typedef pair<int,int> PII;//#define HOMEint Scan(){int res = 0, ch, flag = 0;if((ch = getchar()) == '-')//判断正负flag = 1;else if(ch >= '0' && ch <= '9')//得到完整的数res = ch - '0';while((ch = getchar()) >= '0' && ch <= '9' )res = res * 10 + ch - '0';return flag ? -res : res;}/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/char str[1000000+5];//int mul[1000000+5];char s[1000000+5];bool judge(){ int len=strlen(str); int l=0,r=0,mul=0,ok=1; int num=0; for(int i=0;i<len;i++) { num++; if(num==1&&str[i]=='?') str[i]='('; if(str[i]=='(') l++; if(str[i]==')') { r++; } if(r>num/2) { ok=0; break; } if(r*2==num) { l=r=num=0; } } if(l>num/2) ok=0; l=r=num=0; strcpy(str,s); for(int i=len-1;i>=0;i--) { num++; if(num==1&&str[i]=='?') str[i]=')'; if(str[i]=='(') l++; if(str[i]==')') r++; if(l>num/2) { ok=0; break; } if(l*2==num) { l=r=num=0; } } if(r>num/2) ok=0; return ok;}int main(){while(scanf("%s",str)!=EOF){ strcpy(s,str); int len=strlen(str); if(judge()==0) { printf("None\n"); continue; } strcpy(str,s); for(int i=0;i<len;i++) { if(str[i]=='?') { strcpy(str,s); str[i]='('; int flag1=judge(); strcpy(str,s); str[i]=')'; int flag2=judge(); if(flag1&&!flag2) s[i]='('; else if(!flag1&&flag2) s[i]=')'; else if(flag1&&flag2) { printf("Many\n"); break; } else { printf("None\n"); break; } } if(i==len-1) { printf("Unique\n"); } }} return 0;}
0 0
- hdu 4915 Parenthese sequence(贪心,模拟)
- hdu 4915 Parenthese sequence (贪心+模拟)
- hdu 4915 Parenthese sequence (贪心+模拟)
- 【HDU】4915 Parenthese sequence 贪心
- HDU 4915 Parenthese sequence
- hdu 4915 Parenthese sequence
- hdu 4915 Parenthese sequence
- HDU 4915 Parenthese sequence
- HDU-4915-Parenthese sequence
- hdu 4915 Parenthese sequence
- HDU 4915 Parenthese sequence
- HDU4915:Parenthese sequence(贪心)
- hdu 4915 Parenthese sequence(多校第5场,括号匹配,贪心算法)
- hdu 4915 Parenthese sequence(高效)
- HDU 4915 Parenthese sequence DP
- hdu 4915 Parenthese sequence(模拟)2014多校训练第5场
- Parenthese sequence -模拟
- HDU 4915 Parenthese sequence(瞎搞题)
- Java常用排序算法/程序员必须掌握的8大排序算法
- ZgukistringZ
- Python核心编程-练习3
- mongoDB快速入门
- shell数组、循环实战演练
- hdu 4915 Parenthese sequence(贪心,模拟)
- 自己写的一个JQUERY焦点图
- Android 录音PCM 转G711U,非常简单,非FFMPEG
- 写出float x 与“零值”比较的if语句。
- NOI2011道路修建 TreeDp
- MySQL 锁
- 员工管理系统ArrayList 更新
- hdu2048 神、上帝以及老天爷
- 这就是观察者模式