POJ 1068 Parencodings【水模拟--数括号】
链接:
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 17044 | Accepted: 10199 |
Description
q By an integer sequence P = p1 p2...pn where pi is the number of left parentheses before the ith right parenthesis in S (P-sequence).
q By an integer sequence W = w1 w2...wn where for each right parenthesis, say a in S, we associate an integer which is the number of right parentheses counting from the matched left parenthesis of a up to a. (W-sequence).
Following is an example of the above encodings:
S (((()()()))) P-sequence 4 5 6666 W-sequence 1 1 1456
Write a program to convert P-sequence of a well-formed string to the W-sequence of the same string.
Input
string. It contains n positive integers, separated with blanks, representing the P-sequence.
Output
Sample Input
2
6
4 5 6 6 6 6
9
4 6 6 6 6 8 9 9 9
Sample Output
1 1 1 4 5 6
1 1 2 4 5 1 1 3 9
Source
题意:
而下面的两个数字序列 P 和 W 都含有 6 个数字,而且都写在这右边6个括号下面
再仔细看序列 P 代表的是从左到右,每一个右括号前面的左括号的个数
平时生活中括号都是一对对存在的
W序列代表的是与当前右括号匹配的最近的左括号编号
【这个编号是按照离开当前需要匹配的右边括号的,左边括号个数计算的】
题目给了你序列 P 要你输出序列 W ,简单模拟一下就好了
算法:
思路:
同时再用一个 vis[] 数组标记已经被匹配过了的左边括号
/**
Accepted 180K 0MS C++ 1586B
题意:先看题目样例中给出的括号序列 S 注意到它有 6 个右括号
而下面的两个数字序列 P 和 W 都含有 6 个数字,而且都写在这右边6个括号下面
再仔细看序列 P 代表的是从左到右,每一个右括号前面的左括号的个数
平时生活中括号都是一对对存在的
W序列代表的是与当前右括号匹配的最近的左括号编号
【这个编号是按照离开当前需要匹配的右边括号的,左边括号个数计算的】 题目给了你序列 P 要你输出序列 W ,简单模拟一下就好了 算法:简单模拟 思路:用一个数组标记即可,左括号标记为 0,右括号标记为 1
同时再用一个 vis[] 数组标记已经被匹配过了的左边括号 */
#include<stdio.h>
#include<string.h>
const int maxn = 2000; int a[maxn]; //记录括号是左边还是右边
int vis[maxn]; // 标记是否被匹配 int main()
{
int T;
int n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
memset(vis, 0, sizeof(vis)); //初始化清空
memset(a, 0, sizeof(a));
int x;
int t = 0; //记录当下左括号数目
int index = 1; //记录当前括号下标
int j;
for(int i = 0; i < n; i++)
{
scanf("%d", &x); //每次输入一个右边括号,同时补全它左边括号的数目
if(x > t)
{
int tmp = x-t; //记录当前左边
t = x;
while(tmp--)
{
a[index++] = 0; //补全左边括号
}
}
a[index++] = 1; //记录当前右边括号
} int flag = 0;
int num = index; // 总的括号数目
//for(int i = 1; i < num; i++) printf("%d ", a[i]); printf("\n");
for(int i = 1; i < num; i++) // 从前往后遍历 )
{
if(a[i] == 1) // )
{
flag = 0;
int ans = 0;
for(int j = i-1; j >= 1; j--) // 从后往前匹配
{
if(flag == 1) break;
if(a[j] == 0 && vis[j] && flag == 0)
{
ans++;
}
else if(a[j] == 0 && !vis[j])
{
ans++;
vis[j] = 1;
printf("%d ", ans);
flag = 1;
break;
}
}
}
}
printf("\n");
}
return 0;
}
POJ 1068 Parencodings【水模拟--数括号】的更多相关文章
- poj 1068 Parencodings(模拟)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj ...
- 模拟 POJ 1068 Parencodings
题目地址:http://poj.org/problem?id=1068 /* 题意:给出每个右括号前的左括号总数(P序列),输出每对括号里的(包括自身)右括号总数(W序列) 模拟题:无算法,s数组把左 ...
- POJ 1068 Parencodings (类似括号的处理问题)
Pare ...
- POJ 1068 Parencodings 模拟 难度:0
http://poj.org/problem?id=1068 #include<cstdio> #include <cstring> using namespace std; ...
- [ACM] POJ 1068 Parencodings(模拟)
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19352 Accepted: 11675 De ...
- poj 1068 Parencodings 模拟题
Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...
- poj 1068 Parencodings 模拟
进入每个' ) '多少前' ( ', 我们力求在每' ) '多少前' ) ', 我的方法是最原始的图还原出来,去寻找')'. 用. . #include<stdio.h> #incl ...
- poj 1068 Parencodings(栈)
题目链接:http://poj.org/problem?id=1068 思路分析:对栈的模拟,将栈中元素视为广义表,如 (((()()()))),可以看做 LS =< a1, a2..., a1 ...
- POJ 1068 Parencodings
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24932 Accepted: 14695 De ...
随机推荐
- 4. Median of Two Sorted Arrays(topK-logk)
4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...
- XShell命令行使用
1.建立连接: 2.查看总体目录: 3.查看对应服务目录: 4.删除对应jar包后,再查看目录下文件: 5.上传对应的jar文件: 6.重启服务 7.查看服务日志: 8.mv old-name new ...
- SparkStreaming数据源Flume实际案例分享
一.什么是Flume? flume 作为 cloudera 开发的实时日志收集系统,受到了业界的认可与广泛应用.Flume 初始的发行版本目前被统称为 Flume OG(original genera ...
- 人工智能真NB?何不去炒股?
自从AlphaGo/Master战胜人类棋手,人工智能替代人工的呼声就甚嚣尘上.人工智能还有个小伙伴名为大数据也来势汹汹,貌似未来二者联手要取代人类似的. 人工智能真的锐不可当?AlphaGo/Mas ...
- Mac OSX下编译安装PostgreSQL
原先使用的是官方提供的安装包,可是安装包会创建postgre这个用户.在登陆界面看的有点不爽,搜索了半天居然没有找到怎样在osx下编译安装的教程,并且假设是依照官方文档的编译安装办法一定会让你崩溃,本 ...
- 微信小程序 - 获取用户信息的几种方式
1. 老接口(上线使用-测试用button先获取用户信息) // 登录 wx.login({ success: res => { // 发送 res.code 到后台换取 openId, ses ...
- poj Muddy Fields
Muddy Fields 原题去我创的专题里找,在文件夹首页. 题目: 给出N*M矩阵.当中*表示泥土,.表示小草.要你用最少的木板把泥土覆盖. 木板长度不限.可是仅仅能水平和竖直. 行列式二分匹配配 ...
- 基于Android平台的简易人脸检测库
代码地址如下:http://www.demodashi.com/demo/12135.html ViseFace 简易人脸检测库,不依赖三方库,可快速接入人脸检测功能. 项目依赖:compile 'c ...
- Hibernate核心类和接口具体介绍
一.hiobernate核心类和接口预览图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGxnZW4xNTczODc=/font/5a6L5L2T/fo ...
- 如何从一个1G的文件中找到你所需要的东西
如何从一个1G的文件中找到你所需要的东西,这个问题貌似面试的时候会经常问到.不过不论你用什么语言,肯定逃脱不了按指针读或者按块读. 这里介绍python的用法.本人亲自实验了,速度还可以. 如果你的文 ...