题目链接: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. python wsgi

    什么是wsgi? wsgi是一个web组件的接口防范,wsgi将web组件分为三类:web服务器,web中间件,web应用程序 wsgi基本处理模式为:wsgi Server -> wsgi m ...

  2. linux线程之pthread_join和pthread_detach

    在任何一个时间点上,线程是可结合的(joinable)或者是分离的(detached).一个可结合的线程能够被其他线程收回其资源和杀死.在 被其他线程回收之前,它的存储器资源(例如栈)是不释放的.相反 ...

  3. 【微信公众号】WeixinJSBridge.call('closeWindow')无效

    公众号上面使用iframe嵌套子页面,然而子页面无法使用WeixinJSBridge.call('closeWindow') 这时候必须在函数前面加上parent.,比如parent.WeixinJS ...

  4. iOS实践04

    第四天 微博数据展示:获取服务器数据,json数据的解析,MVC的使用,自定义cell高度的计算,一些分类的设计.已经是第四天了,虽然每天都有课程,但这个东西也基本完成了一大半吧,一些忘掉的知识也捡起 ...

  5. ubuntu下使用codeblocks

    集成开发环境搭建 1. 安装build-essential 方法: sudo apt-get install build-essential 作用:提供编译程序必须软件包的列表信息,编译程序有了这个软 ...

  6. BZOJ 1601: [Usaco2008 Oct]灌水( MST )

    MST , kruskal 直接跑 ---------------------------------------------------------------------- #include< ...

  7. 解决spark运行中failed to locate the winutils binary in the hadoop binary path的问题

    1.下载hadoop-common-2.2.0-bin并解压到某个目录 https://github.com/srccodes/hadoop-common-2.2.0-bin 2.设置hadoop.h ...

  8. [LeetCode]题解(python):133-Clone Graph

    题目来源: https://leetcode.com/problems/clone-graph/ 题意分析: 克隆一个无向图.每个节点包括一个数值和它的邻居. 题目思路: 直接深度拷贝. 代码(pyt ...

  9. 哈夫曼树(Huffman)的JS实现

    我本身并不懂哈夫曼树也不知道有什么用,GOOGLE了下,也只是一知半解,只是刚好看到有JAVA实现版,又看了下生成原理,感觉挺有意思,就写了一下 有些地方可以优化,效率不怎么样的,纯好玩,也不保证一定 ...

  10. C Looooops(扩展欧几里德)

    C Looooops Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...