poj 1068 Parencodings(栈)
题目链接:http://poj.org/problem?id=1068
思路分析:对栈的模拟,将栈中元素视为广义表,如 (((()()()))),可以看做 LS =< a1, a2..., a12 >,对于可以配对的序列,如 <a4, a5>看做一个元素,其 W 值为1;
同理,<a6, a7>为一个元素,其W值为1,< a3, a4, a5, a6, a7, a8, a9, a10 >看做一个元素, 其W值为 <a4, a5> 与 <a6, a7>、< a8, a9 >的W的值的和加 1,即为4;
如此处理,直到求出所有的W值。
代码如下:
- #include <iostream>
- #include <stack>
- using namespace std;
- int main()
- {
- int n;
- char Record[]; // 广义表记录
- stack<char> A;
- cin >> n;
- for ( int i = ; i < n; ++i )
- {
- int Size, Index = -;
- int Count1 = , Count2;
- cin >> Size;
- for( int j = ; j < Size; ++j )
- {
- int W = , IsMatch = ;
- cin >> Count2;
- for ( int k = ; k < Count2 - Count1; ++k )
- A.push('(');
- while ( IsMatch == )
- {
- if ( A.top() == ')' )
- {
- A.pop();
- W += Record[Index--];
- }
- else
- {
- A.pop();
- IsMatch = ;
- }
- }
- A.push(')');
- Record[++Index] = W;
- Count1 = Count2;
- printf("%d ", W );
- }
- printf( "\n" );
- }
- return ;
- }
poj 1068 Parencodings(栈)的更多相关文章
- 模拟 POJ 1068 Parencodings
题目地址:http://poj.org/problem?id=1068 /* 题意:给出每个右括号前的左括号总数(P序列),输出每对括号里的(包括自身)右括号总数(W序列) 模拟题:无算法,s数组把左 ...
- POJ 1068 Parencodings【水模拟--数括号】
链接: http://poj.org/problem?id=1068 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...
- POJ 1068 Parencodings 模拟 难度:0
http://poj.org/problem?id=1068 #include<cstdio> #include <cstring> using namespace std; ...
- POJ 1068 Parencodings (类似括号的处理问题)
Pare ...
- poj 1068 Parencodings(模拟)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj ...
- POJ 1068 Parencodings
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24932 Accepted: 14695 De ...
- [ACM] POJ 1068 Parencodings(模拟)
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19352 Accepted: 11675 De ...
- poj 1068 Parencodings 模拟
进入每个' ) '多少前' ( ', 我们力求在每' ) '多少前' ) ', 我的方法是最原始的图还原出来,去寻找')'. 用. . #include<stdio.h> #incl ...
- poj 1068 Parencodings 模拟题
Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...
随机推荐
- JS 在html中的位置
前言 当我了解完html在浏览器中的解析渲染流程后,反而又发现了新的困扰自己的问题. Q:即然html要渲染需要渲染树,而渲染树又需要DOMTree和CSSRuleTree,DOMTree需要解析HT ...
- Unity cg vertex and fragment shaders(二)
着色器的一般结构: Shader "MyShader/MyShaderName" { Properties { // ... properties here ... } SubSh ...
- C++面向对象类的书写相关细节梳理
类的问题 继承类的原因:为了添加或者替换功能. 1. 继承时重写类的方法 v 替换功能 ① 将所有方法都设置为virtual(虚函数),以防万一. Virtual:经验表明最好将所有方法都设置为vir ...
- Problem E: Product
Problem E: ProductTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 18 Solved: 14[Submit][Status][Web Bo ...
- 51NOD 算法马拉松8
题目戳这里:51NOD算法马拉松8 某天晚上kpm在玩OSU!之余让我看一下B题...然后我就被坑进了51Nod... A.还是01串 水题..怎么乱写应该都可以.记个前缀和然后枚举就行了.时间复杂度 ...
- css伪类伪元素
在CSS中,模式(pattern)匹配规则决定哪种样式规则应用于文档树(document tree)的哪个元素.这些模式叫着选择符(selector). 一条CSS规则(rule)是选择符{属性:值; ...
- A Byte of Python 笔记(7)数据结构:列表、元组、字典,序列
第9章 数据结构 数据结构,即可以处理一些数据的结构.或者说,它们是用来存储一组相关数据的. python 有三种内建的数据结构--列表.元组和字典. list = ['item1', 'item2' ...
- [LeetCode]题解(python):085-Maximal Rectangle
题目来源: https://leetcode.com/problems/maximal-rectangle/ 题意分析: 给定一个二维的二进制矩阵,也就是只包括0 和 1的,找出只包括1的最大的矩阵的 ...
- java json的处理
maven依赖 <dependencies> <dependency> <groupId>com.alibaba</groupId> <artif ...
- 启用Apache Mod_rewrite模块
Ubuntu 环境 在终端中执行 sudo a2enmod rewrite 指令后,即启用了 Mod_rewrite 模块. 另外,也可以通过将 /etc/apache2/mods-available ...