poj 1068 Parencodings(模拟)
2013-07-24 09:58
351 查看
题目链接:http://poj.org/problem?id=1068
题目大意:
例如有一个关于括号的符号串(由'('和')'组成)S;
现在给出数列p代表,符号串中每个右括号的左边有几个左括号;
要求求出数列w代表,每个右括号和其相匹配的左括号中包含有几组匹配括号(包含自身);
思路:
利用栈存储字符串S,在利用一个临时的栈来倒序分组求出每个匹配括号中包含的匹配括号数;
题目大意:
例如有一个关于括号的符号串(由'('和')'组成)S;
现在给出数列p代表,符号串中每个右括号的左边有几个左括号;
要求求出数列w代表,每个右括号和其相匹配的左括号中包含有几组匹配括号(包含自身);
思路:
利用栈存储字符串S,在利用一个临时的栈来倒序分组求出每个匹配括号中包含的匹配括号数;
#include<iostream> #include<stack> using namespace std; stack <char> temp; stack <char> S; int main() { int cas; int ret[21]; scanf("%d",&cas); while(cas--) { int n,i,num,t=0; scanf("%d",&n); int len=n; for(i=0;i<n;i++) { scanf("%d",&num); while(t<num) { S.push('('); t++; } S.push(')'); } n--; while(!S.empty()) { S.pop(); int right=1,left=0; while(!S.empty()) { if(S.top()==')') right++; else left++; temp.push(S.top()); S.pop(); if(right==left) { ret[n--]=left; break; } } temp.pop(); while(!temp.empty()) { S.push(temp.top()); temp.pop(); } } for(i=0;i<len;i++) { if(i==len-1) printf("%d\n",ret[i]); else printf("%d ",ret[i]); } } return 0; }
相关文章推荐
- POJ 1068 Parencodings(小模拟)
- poj1068 Parencodings 【模拟】
- poj_1068 Parencodings(简单模拟+位运算)
- HDU 1361 & POJ 1068 Parencodings(模拟)
- poj 1068 Parencodings(模拟)
- POJ 1068-Parencodings(模拟)
- 【模拟】POJ-1068-Parencodings
- POJ 1068 Parencodings 模拟
- poj 1068 Parencodings (模拟)
- poj 1068 Parencodings(模拟)
- POJ - 1068 Parencodings 模拟
- [ACM] POJ 1068 Parencodings(模拟)
- poj 1068 Parencodings(模拟)
- POJ1068 Parencodings(模拟)
- POJ 1068 Parencodings 模拟 栈操作
- poj 1068 Parencodings(模拟)
- POJ 1068 Parencodings(模拟)
- poj 1068 Parencodings——模拟
- poj 1068 Parencodings 模拟
- poj 1068 Parencodings (模拟)