zoukankan      html  css  js  c++  java
  • 【编程练习】poj1068

    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("
    ");
    
    		free(array);
    		free(rarray);
    	}
    
    }


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

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

  • 相关阅读:
    【原生】数组去重的方法
    【原生】详解Javascript中prototype属性(推荐)
    【原生】 ES5中的构造函数与 ES6 中的类 及构造函数的理解
    【react】React学习:状态(State) 和 属性(Props)
    【react】 flux 了解
    【请求数据】fetch 的get请求和post请求
    【react】 使用create-react-app创建react项目,运行npm run eject报错: 暴露webpack配置文件
    package.json 文件的介绍
    802.11协议精读1:学习资料整理(转)
    ns2之包结构解析
  • 原文地址:https://www.cnblogs.com/wuyida/p/6301273.html
Copyright © 2011-2022 走看看