题目链接:

https://vjudge.net/problem/POJ-1068

题目大意:

给出一种括号序列的表示形式名叫P序列,规则是统计出每个右括号之前的左括号个数作为序列每项的值。然后要求你根据这个求括号列的W序列值,W序列的规则是统计每一个右括号和与其匹配的左括号之间所有匹配后的括号个数。

思路:

先模拟出括号序列,然后根据每个右括号向前入栈,记录中间的括号数目

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#define MEM(s, b) memset(a, b, sizeof(a));
using namespace std;
typedef long long ll;
int T, n, a[], b[], ans[];
int main()
{ cin >> T;
while(T--)
{
cin >> n;
string s;
MEM(a, );
MEM(b, );
MEM(ans, );
for(int i = ; i <= n; i++)cin >> a[i];
for(int i = ; i <= n; i++)
{
for(int j = ; j <= a[i] - a[i - ]; j++)s += '(';
s += ')';
}
// cout<<s<<endl;
int tot = ;
for(int i = ; i < s.size(); i++)
{
if(s[i] == ')')b[tot++] = i;
}
for(int i = ; i < tot; i++)
{
stack<char>q;
int cnt = ;
for(int j = b[i]; j >= ; j--)
{
if(s[j] == ')')q.push(s[j]);
else
{
q.pop();
cnt++;
if(q.empty())break;
}
}
ans[i] = cnt;
}
cout<<ans[];
for(int i = ; i < tot; i++)cout<<" "<<ans[i];
cout<<endl;
}
return ;
}

POJ-1068 Parencodings---模拟括号的配对的更多相关文章

  1. POJ 1068 Parencodings 模拟 难度:0

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

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

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

  3. poj 1068 Parencodings 模拟题

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

  4. poj 1068 Parencodings 模拟

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

  5. 模拟 POJ 1068 Parencodings

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

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

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

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

                                                                                                    Pare ...

  8. poj 1068 Parencodings(模拟)

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

  9. poj 1068 Parencodings(栈)

    题目链接:http://poj.org/problem?id=1068 思路分析:对栈的模拟,将栈中元素视为广义表,如 (((()()()))),可以看做 LS =< a1, a2..., a1 ...

  10. POJ 1068 Parencodings

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

随机推荐

  1. Mycat 配置说明(schema.xml )

    Schema.xml 作为 MyCat中重要的配置文件之一,管理着 MyCat的逻辑库.表.分片规则.DataNode以 及DataSource.弄懂这些配置,是正确使用MyCat的前提. schem ...

  2. vue技术解析六之生命周期函数

  3. Android开发从GC root分析内存泄漏

    我们常说的垃圾回收机制中会提到GC Roots这个词,也就是Java虚拟机中所有引用的根对象.我们都知道,垃圾回收器不会回收GC Roots以及那些被它们间接引用的对象.但是,对于GC Roots的定 ...

  4. nxlog4go 的配置驱动

    刚开始接触log4go项目时,没有注意到配置的重要性. 阅读了log4j.log4net.log4cpp.log4cplus的部分代码,发现它们都是以xml配置来驱动日志系统运行的. 多个源文件共享一 ...

  5. STL --> remove和remove_if()

    remove和remove_if() 一.Remove()函数 remove(beg,end,const T& value) //移除区间{beg,end)中每一个“与value相等”的元素: ...

  6. hibernate学习之持久化对象

    Hibernate对其持久化对象实现了缓存管理,来提高系统性能,Hibernate支持两级缓存管理,一级缓存 是由Session提供的,因此它只存在于Session的生命周期中,是Session所内置 ...

  7. java 5并发中的阻塞队列ArrayBlockingQueue的使用以及案例实现

    演示一个阻塞队列的使用 public class BlockingQueueTest { public static void main(String[] args) { //创建一个包含三个元素的阻 ...

  8. Beta阶段敏捷冲刺报告-DAY3

    Beta阶段敏捷冲刺报告-DAY3 Scrum Meeting 敏捷开发日期 2017.11.4 会议时间 12:30 会议地点 软工所 参会人员 全体成员 会议内容 当天任务确认,进度调整, 讨论时 ...

  9. 卡尔曼滤波法C编程

    float Angle = 0.0;//卡尔曼滤波器的输出值,最优估计的角度 //float Gyro_x = 0.0;//卡尔曼滤波器的输出值,最优估计的角速度 float Q_angle = 0. ...

  10. Android 4.4 沉浸式透明状态栏

    原文链接:http://www.bkjia.com/Androidjc/913061.html 第一种方法 这里写代码片第一种方法,在代码设置: if(VERSION.SDK_INT >= VE ...