【编程练习】poj1068
来源:互联网 发布:淘宝卡哪个好 编辑:程序博客网 时间:2024/06/08 07:20
Parencodings
Time Limit: 1000MS Memory Limit: 10000KTotal 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:
Write a program to convert P-sequence of a well-formed string to the W-sequence of the same string.
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 6666W-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
264 5 6 6 6 69 4 6 6 6 6 8 9 9 9
Sample Output
1 1 1 4 5 61 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
1 0
- 【编程练习】poj1068
- POJ1068
- POJ1068
- Poj1068
- poj1068
- poj1068
- poj1068
- poj1068
- poj1068
- poj1068
- poj1068
- POJ1068
- poj1068
- poj1068
- poj1068
- POJ1068
- poj1068
- poj1068
- android学习第一天:测试
- 练习二1001
- 2016SDAU编程练习二1024
- C和C++的读入方式对比
- 下蹲运动的好处与练习方式
- 【编程练习】poj1068
- 【usaco 2013 Mar Bronze】
- Linux的SOCKET编程详解
- 在android中实现多线程下载和断点续传
- 面向对象三大特性——封装
- ScrollView嵌套ListView只显示一行
- 在IDEA中使用MyBatis Generator逆向工程生成代码
- "围观"设计模式(2)--里氏替换原则(LSP,Liskov Substitution Principle)
- Android Studio 2.0+发布APK出现ExternalSystemException: String index out of range: -123错误