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. 为了异常安全(swap,share_ptr)——Effecive C++

    互斥锁: 假设我们要在多线程中实现背景图片的控制: class PrettyMenu{ public: -- void changeBackground(std::istream& imgSr ...

  2. ●UVA 11796 Dog Distance

    题链: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. NOIP 2013

    Prob.1 转圈游戏 找到循环节,然后快速幂.代码: #include<cstdio> #include<cstring> #include<iostream> ...

  4. vim配置文件和插件管理

    本文通过总结零碎的资料总结而成,更多是去引导学习vim配置文件及插件使用. .vimrc配置文件,内容如下(备注清晰) "引入插件pathogen使用 execute pathogen#in ...

  5. Java文件操作(新建,遍历,删除)

    //创建文件 private static void createFile(File file){ try { if(!file.exists()){ if(file.getParentFile() ...

  6. C# 解决winform 窗体控件在窗体变化时闪烁的问题

    在窗体form代码中加入如下代码即可: protected override CreateParams CreateParams { get { CreateParams cp = base.Crea ...

  7. Python中的文件路径的分隔符

    主要是需要考虑分隔符的问题: 在Windows系统下的分隔符是:\ (反斜杠). 在Linux系统下的分隔符是:/(斜杠). 当在字符中出现\时,大家就要考虑到转义字符了. 转义字符的概念,参考维基百 ...

  8. 从 vCenter Server 使用的数据库中清除旧数据 (2075138)(转)

    Document Id 2075138 Symptoms 免责声明: 本文为 Purging old data from the database used by VMware vCenter Ser ...

  9. Java正则过滤

    import java.util.regex.Matcher; import java.util.regex.Pattern; public class LongStringtonumber { pu ...

  10. 关于thymeleaf th:replace th:include th:insert 的区别

    关于thymeleaf th:replace th:include th:insert 的区别    th:insert   :保留自己的主标签,保留th:fragment的主标签.    th:re ...