题目链接: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值。

代码如下:   

  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int n;
  8. char Record[]; // 广义表记录
  9. stack<char> A;
  10.  
  11. cin >> n;
  12. for ( int i = ; i < n; ++i )
  13. {
  14. int Size, Index = -;
  15. int Count1 = , Count2;
  16.  
  17. cin >> Size;
  18. for( int j = ; j < Size; ++j )
  19. {
  20. int W = , IsMatch = ;
  21.  
  22. cin >> Count2;
  23. for ( int k = ; k < Count2 - Count1; ++k )
  24. A.push('(');
  25.  
  26. while ( IsMatch == )
  27. {
  28. if ( A.top() == ')' )
  29. {
  30. A.pop();
  31. W += Record[Index--];
  32. }
  33. else
  34. {
  35. A.pop();
  36. IsMatch = ;
  37. }
  38. }
  39.  
  40. A.push(')');
  41. Record[++Index] = W;
  42. Count1 = Count2;
  43.  
  44. printf("%d ", W );
  45. }
  46. printf( "\n" );
  47. }
  48.  
  49. return ;
  50. }

poj 1068 Parencodings(栈)的更多相关文章

  1. 模拟 POJ 1068 Parencodings

    题目地址:http://poj.org/problem?id=1068 /* 题意:给出每个右括号前的左括号总数(P序列),输出每对括号里的(包括自身)右括号总数(W序列) 模拟题:无算法,s数组把左 ...

  2. POJ 1068 Parencodings【水模拟--数括号】

    链接: http://poj.org/problem?id=1068 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...

  3. POJ 1068 Parencodings 模拟 难度:0

    http://poj.org/problem?id=1068 #include<cstdio> #include <cstring> using namespace std; ...

  4. POJ 1068 Parencodings (类似括号的处理问题)

                                                                                                    Pare ...

  5. poj 1068 Parencodings(模拟)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj ...

  6. POJ 1068 Parencodings

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24932   Accepted: 14695 De ...

  7. [ACM] POJ 1068 Parencodings(模拟)

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19352   Accepted: 11675 De ...

  8. poj 1068 Parencodings 模拟

    进入每个' )  '多少前' (  ', 我们力求在每' ) '多少前' )  ', 我的方法是最原始的图还原出来,去寻找')'. 用. . #include<stdio.h> #incl ...

  9. poj 1068 Parencodings 模拟题

    Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...

随机推荐

  1. JS 在html中的位置

    前言 当我了解完html在浏览器中的解析渲染流程后,反而又发现了新的困扰自己的问题. Q:即然html要渲染需要渲染树,而渲染树又需要DOMTree和CSSRuleTree,DOMTree需要解析HT ...

  2. Unity cg vertex and fragment shaders(二)

    着色器的一般结构: Shader "MyShader/MyShaderName" { Properties { // ... properties here ... } SubSh ...

  3. C++面向对象类的书写相关细节梳理

    类的问题 继承类的原因:为了添加或者替换功能. 1. 继承时重写类的方法 v 替换功能 ① 将所有方法都设置为virtual(虚函数),以防万一. Virtual:经验表明最好将所有方法都设置为vir ...

  4. Problem E: Product

    Problem E: ProductTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 18 Solved: 14[Submit][Status][Web Bo ...

  5. 51NOD 算法马拉松8

    题目戳这里:51NOD算法马拉松8 某天晚上kpm在玩OSU!之余让我看一下B题...然后我就被坑进了51Nod... A.还是01串 水题..怎么乱写应该都可以.记个前缀和然后枚举就行了.时间复杂度 ...

  6. css伪类伪元素

    在CSS中,模式(pattern)匹配规则决定哪种样式规则应用于文档树(document tree)的哪个元素.这些模式叫着选择符(selector). 一条CSS规则(rule)是选择符{属性:值; ...

  7. A Byte of Python 笔记(7)数据结构:列表、元组、字典,序列

    第9章 数据结构 数据结构,即可以处理一些数据的结构.或者说,它们是用来存储一组相关数据的. python 有三种内建的数据结构--列表.元组和字典. list = ['item1', 'item2' ...

  8. [LeetCode]题解(python):085-Maximal Rectangle

    题目来源: https://leetcode.com/problems/maximal-rectangle/ 题意分析: 给定一个二维的二进制矩阵,也就是只包括0 和 1的,找出只包括1的最大的矩阵的 ...

  9. java json的处理

    maven依赖 <dependencies> <dependency> <groupId>com.alibaba</groupId> <artif ...

  10. 启用Apache Mod_rewrite模块

    Ubuntu 环境 在终端中执行 sudo a2enmod rewrite 指令后,即启用了 Mod_rewrite 模块. 另外,也可以通过将 /etc/apache2/mods-available ...