题目链接: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(栈)的更多相关文章

  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. SSO 基于Cookie+fliter实现单点登录(SSO):工作原理

    SSO的概念: 单点登录SSO(Single Sign-On)是身份管理中的一部分. SSO的一种较为通俗的定义是:SSO是指訪问同一server不同应用中的受保护资源的同一用户,仅仅须要登录一次,即 ...

  2. 【LINUX】SHELL syntax error:unexpected end of file

    解决思路: DOS下文件和Linux下文件格式差异问题导致的. DOS下的文本文件是以\r\n作为断行标志的,表示成十六进制就是0D 0A.而Unix下的文本文件是以\n作为断行标志的,表示成十六进制 ...

  3. preg_replace的用法

    <?php $str1 = "03/28/2015"; // 要替换成 2015-03-28 echo preg_replace("/([0-1][1-9])\/( ...

  4. Centos rpm缺少依赖无法安装mysql5.5

    rpm -ivh mysql-5.5.22-2.1.i386.rpm --nodeps --force 缺少依赖导致rpm -ivh mysql-5.5.22-2.1.i386.rpm命令无法安装!

  5. codeforces 622E. Ants in Leaves

    题目链接 给一棵有根树, 每个叶子节点上有一只蚂蚁. 在0时刻蚂蚁开始向上爬, 同一时刻, 除了根节点以外, 一个节点上面不能有2个蚂蚁. 问所有的蚂蚁都爬到根节点需要的最短时间. 因为除了根节点, ...

  6. [LeetCode]题解(python):106-Construct Binary Tree from Inorder and Postorder Traversal

    题目来源: https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意分析 ...

  7. python函数abs()

    详解: 返回绝对值 参数可以是:负数.正数.浮点数或者长整形 实例: abs(-1.2) #返回 1.2 abs(1.2) #返回 1.2 abs(-11216.5) #返回 11216.5 abs( ...

  8. ASP.NET jQuery 随笔 显示RadioButtonList成员选中的内容和值

    通过jQuery来获取RadioButtonList成员内容. <%@ Page Language="C#" AutoEventWireup="true" ...

  9. javascript收集整理

    //特殊字符去掉 function check(obj){ var test=obj.value.match(/[^A-Za-z0-9]/g);//这条语句表示在输入文本中是否包含不符合要求的字符,如 ...

  10. Inlay技术要求

    物理特性: 项目 要求内容 备考 基准值 公差 INLAY尺寸 A(长) 480mm ±0.5mm B(宽) 380mm ±0.5mm 线圈位置 C(天地位置) 16.05mm ±0.2mm D(左右 ...