[JAVA][ZOJ 1016][Parencodings]

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

  • 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).
  • 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 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

[java] view
plain
copyprint?

  1. <p>
  2. </p>import java.io.BufferedInputStream;
  3. import java.util.Scanner;
  4. public class Main {
  5. public static void main(String[] args) throws InterruptedException {
  6. Scanner sc = new Scanner(System.in);
  7. int n = sc.nextInt();
  8. for (int i = 0; i < n; i++) {
  9. int len = sc.nextInt();
  10. int P[] = new int[len];// P shows how many left parentheses have been seen since each right parentheses
  11. int W[] = new int[len];// W shows how many left parentheses have been seen within a pair of parentheses
  12. // EG:<><>,W=11
  13. // EG:<<>>,W=12
  14. // EG:<<<><>>>,W=1134
  15. char str[] = new char[len * 2];
  16. int posInP = 0;
  17. for (int posInStr = 0; posInStr < len; posInStr++) {
  18. P[posInStr] = sc.nextInt();
  19. if (posInStr == 0) {// the first P-code
  20. for (; posInP < P[posInStr]; posInP++) {
  21. str[posInP] = ‘<‘;
  22. }
  23. str[posInP++] = ‘>‘;
  24. } else {
  25. if (P[posInStr] > P[posInStr - 1]) {
  26. for (int k = 0; k < (P[posInStr] - P[posInStr - 1]); k++) {
  27. str[posInP++] = ‘<‘;
  28. }
  29. str[posInP++] = ‘>‘;
  30. } else {// if the next num is the same of the last
  31. str[posInP++] = ‘>‘;
  32. }
  33. }
  34. }
  35. // check for W array
  36. int posInW = 0;
  37. for (int j = 0; j < len * 2; j++) {
  38. if (str[j] == ‘<‘) {
  39. continue;
  40. }
  41. int RightParentheses = 1;
  42. for (int j2 = j - 1; j2 >= 0; j2--) {
  43. if (str[j2] == ‘>‘) {
  44. RightParentheses++;
  45. } else {
  46. RightParentheses--;
  47. W[posInW]++;
  48. }
  49. if (RightParentheses == 0) {
  50. posInW++;
  51. break;
  52. }
  53. }
  54. }
  55. int j;
  56. for (j = 0; j < len - 1; j++) {
  57. System.out.print(W[j] + " ");
  58. }
  59. System.out.print(W[j]);
  60. System.out.println();
  61. }
  62. }
  63. }

PE了好多次,在Eclipse中复制2个testcase,刚复制完就出现第一个的结果,然后按回车就出现另一个结果,本来以为是这个问题,改为把所有的结果收集然后集中输出后又显示RE,最后发现居然是输出的问题,最后一个数字后面不输出空格,这细节真心不是坑爹嘛。。。。

时间: 2024-08-29 12:27:42

[JAVA][ZOJ 1016][Parencodings]的相关文章

ZOJ 1016 Parencodings

Parencodings 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=16 比较简单,直接在该位置往前找,找到能匹配的,越过的右括号+1即为所求答案. 1 //Problem Name:Parencodings 2 //Source: ZOJ 3 //Author: jinjin18 4 //Main idea: 5 //Language: C++ 6 //============================

[JAVA][ZOJ 1004][Anagrams by Stack]

[java] view plaincopyprint? import java.io.BufferedInputStream; import java.util.Scanner; public class Main { static String start;// record the first str static String end;// record the rearranged str public static void dfs(char[] stack, char[] seque

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

?--Porg.springframework.beans.MethodInvocationException: Property &#39;username&#39; threw exception; nested exception is java.lang.NullPointerException

使用BoneCP作为连接池,在启动Tomcat报出以下异常: 一月 02, 2016 2:12:17 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:mynewdpi' did not find a

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

bug_ _org.json.JSONException: End of input at character 0 of

10-16 18:28:39.549: W/System.err(4950): org.json.JSONException: End of input at character 0 of 10-16 18:28:39.559: W/System.err(4950): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)10-16 18:28:39.569: W/System.err(4950): at org.json.JSONTo

Tomcat单机多实例配置

配置多实例目录: 安装好tomcat后,创建三个实例目录,将主站点的:conf. work .temp.logs目录分别复制到3个实例目录下. 1 [[email protected] tomcat]# mkdir {bbs.fox.com,www.fox.com,e-mail.fox.com} 2 [[email protected] tomcat]# cp -r conf logs temp work ./bbs.fox.com 3 [[email protected] tomcat]# c

zoj Fibonacci Numbers ( java , 简单 ,大数)

题目 //f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2) import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @xqq */ public BigInteger an(int n) { BigInteger c; BigInteger a = BigInteger.valueOf(1); BigInteger b = BigIn

zoj 3714 Java Beans

#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<queue> #include<stack> #define mem(a,b) memset(a,b,sizeof(a)) #define ll __int64 #define MAXN 1000 #define INF