Parencodings
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24202   Accepted: 14201

Description

Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: 

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

The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case is an integer n (1 <= n <= 20), and the second line is the P-sequence of a well-formed
string. It contains n positive integers, separated with blanks, representing the P-sequence.

Output

The output file consists of exactly t lines corresponding to test cases. For each test case, the output line should contain n integers describing the W-sequence of the string corresponding to its given P-sequence.

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

http://blog.csdn.net/xinghongduo/article/details/6174671

http://blog.chinaunix.net/uid-22609852-id-3506161.html

ac代码:

#include <stdio.h>
#include <stdlib.h>
char c_kuohao[10000] = {0}; //生成空格匹配的字符串
void genkuohao(char* c_kuohao,int* array,int arraylength )
{
int cur_index = 0;
for (int i = 0; i< arraylength-1;i++)
{
int j ;
for ( j = 0;j <*(array+i+1)- *(array +i);j++)
{
c_kuohao[cur_index + j] = '('; }
c_kuohao[cur_index + j ] = ')';
cur_index = cur_index + j +1;
}
} //从括号字符串中,获取int 数组
//找到一个右括号,把匹配最近的左括号设置为字符1,并生成对应的rarray数组
void getWarray(char* c_kuohao,int* rarray,int arraylength)
{
int index = 0;
int i = 0;
while(c_kuohao[i]!=0)
{
if (c_kuohao[i] ==')')
{
int j = i-1;
while(c_kuohao[j]!='(')
{
j--;
if (c_kuohao[j] == '1')
{
*(rarray + index) += 1;
} }
*(rarray + index) += 1;
c_kuohao[j] ='1' ;
index++;
i++;
}
else
{
i++;
} }
} void main()
{ //freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout); /* 同控制台输入输出 */
int mainIndex = 0;
scanf("%d",&mainIndex); for (int i = 0; i < mainIndex;i++)
{ int N = 0;
scanf("%d",&N);
// 下面申请内存时候要用sizeof不然free时候会算错导致堆出错
int *array = (int*)malloc(sizeof(int)*(N +1));
int *rarray = (int*)malloc(sizeof(int)*N);
//给数组第一个位置放个0
*(array+0) = 0; for (int j = 1;j<=N;j++)
{
scanf("%d",array+j);
*(rarray + j-1) =0; } for (int k = 0;k<10000;k++)
{
c_kuohao[k] = 0;
} genkuohao(c_kuohao,array,N+1);
getWarray(c_kuohao,rarray,N); for (int z = 0;z<N;z++)
{
printf("%d ",*(rarray + z));
} printf("\n"); free(array);
free(rarray);
} }

再分享一个非常短的代码:

http://blog.csdn.net/qingniaofy/article/details/7701626

【编程练习】poj1068的更多相关文章

  1. 从直播编程到直播教育:LiveEdu.tv开启多元化的在线学习直播时代

    2015年9月,一个叫Livecoding.tv的网站在互联网上引起了编程界的注意.缘于Pingwest品玩的一位编辑在上网时无意中发现了这个网站,并写了一篇文章<一个比直播睡觉更奇怪的网站:直 ...

  2. JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  3. 读书笔记:JavaScript DOM 编程艺术(第二版)

    读完还是能学到很多的基础知识,这里记录下,方便回顾与及时查阅. 内容也有自己的一些补充. JavaScript DOM 编程艺术(第二版) 1.JavaScript简史 JavaScript由Nets ...

  4. [ 高并发]Java高并发编程系列第二篇--线程同步

    高并发,听起来高大上的一个词汇,在身处于互联网潮的社会大趋势下,高并发赋予了更多的传奇色彩.首先,我们可以看到很多招聘中,会提到有高并发项目者优先.高并发,意味着,你的前雇主,有很大的业务层面的需求, ...

  5. C#异步编程(一)

    异步编程简介 前言 本人学习.Net两年有余,是第一次写博客,虽然写的很认真,当毕竟是第一次,肯定会有很多不足之处, 希望大家照顾照顾新人,有错误之处可以指出来,我会虚心接受的. 何谓异步 与同步相对 ...

  6. UE4新手之编程指南

    虚幻引擎4为程序员提供了两套工具集,可共同使用来加速开发的工作流程. 新的游戏类.Slate和Canvas用户接口元素以及编辑器功能可以使用C++语言来编写,并且在使用Visual Studio 或 ...

  7. C#与C++的发展历程第三 - C#5.0异步编程巅峰

    系列文章目录 1. C#与C++的发展历程第一 - 由C#3.0起 2. C#与C++的发展历程第二 - C#4.0再接再厉 3. C#与C++的发展历程第三 - C#5.0异步编程的巅峰 C#5.0 ...

  8. 猫哥网络编程系列:HTTP PEM 万能调试法

    注:本文内容较长且细节较多,建议先收藏再阅读,原文将在 Github 上维护与更新. 在 HTTP 接口开发与调试过程中,我们经常遇到以下类似的问题: 为什么本地环境接口可以调用成功,但放到手机上就跑 ...

  9. 关于如何提高Web服务端并发效率的异步编程技术

    最近我研究技术的一个重点是java的多线程开发,在我早期学习java的时候,很多书上把java的多线程开发标榜为简单易用,这个简单易用是以C语言作为参照的,不过我也没有使用过C语言开发过多线程,我只知 ...

随机推荐

  1. POJ1222熄灯问题

    千年老题,以前用枚举做,现在用高斯消元做 自由元直接做成0即可 #include<cstdio> #include<cstdlib> #include<algorithm ...

  2. hdu 5607 BestCoder Round #68 (矩阵快速幂)

    graph  Accepts: 9 Submissions: 61  Time Limit: 8000/4000 MS (Java/Others)  Memory Limit: 65536/65536 ...

  3. 配置mysql使其允许外部ip进行登录

    这两天在做一个数据库的项目,用到了mysql,需要通过外部的ip远程访问mysql,发现默认的mysql是不允许远程访问的,需要做简单的配置. 如下: 1. 打开一个终端(cmd)输入 mysql - ...

  4. python 用codecs实现数据的读取

    import numpy as np import codecs f=codecs.open('testsklearn.txt','r','utf-8').readlines() print(f) d ...

  5. [ Java学习基础 ] Java的继承与多态

    看到自己写的东西(4.22的随笔[ Java学习基础 ] Java构造函数)第一次达到阅读100+的成就还是挺欣慰的,感谢大家的支持!希望以后能继续和大家共同学习,共同努力,一起进步!共勉! ---- ...

  6. C语言第五次作业——循环结构

    C语言程序设计第五次作业--循环结构(1) (一)改错题 输出华氏摄氏温度转换表:输入两个整数lower和upper,输出一张华氏摄氏温度转换表,华氏温度的取值范围是{lower,upper},每次增 ...

  7. logback学习二

    转载:https://www.cnblogs.com/DeepLearing/p/5663178.html 属性 : debug : 默认为false ,设置为true时,将打印出logback内部日 ...

  8. 各种电子面单Api接口免费对接-快宝开放平台

    1.什么是电子面单? 快递公司联合向商家提供的一种通过热敏纸打印输出纸质物流面单的物流服务,并且承载分单自动化算法等数据服务,是快递行业赋能的基础产品和服务. 2.电子面单长什么样? 各快递公司有自己 ...

  9. 蚂蚁代理免费代理ip爬取(端口图片显示+token检查)

    分析 蚂蚁代理的列表页大致是这样的: 端口字段使用了图片显示,并且在图片上还有各种干扰线,保存一个图片到本地用画图打开观察一下: 仔细观察蓝色的线其实是在黑色的数字下面的,其它的干扰线也是,所以这幅图 ...

  10. CentOS 安装Docker

    CentOS 系列安装 Docker Docker 支持 CentOS6 及以后的版本. CentOS6 对于 CentOS6,可以使用 EPEL 库安装 Docker,命令如下 $ sudo yum ...