poj1068 Parencodings【模拟】【刷题计划】

Parencodings

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 27375   Accepted: 16094

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

题意:给你t组数据,每组数据输入一个n,再输入n个数,表示第i个右括号的左边有pi个左括号,比如题目上的 4 5 6 6 6 6,第1个右括号的左边有4个左括号,第2个右括号的左边有5个左括号,以此类推,要求输出的是,第i个右括号的左边包含多少个已经与左括号匹配好的完整括号,比如题目上的1 1 1 4 5 6 ,在第1个右括号的左边有1个左括号与之匹配,所以第1个右括号的左边有1个完整匹配的括号,后边两个1也是这样推出来的,直到第4个右括号,它和从左到右数的第3个左括号匹配,而它们中间包括了3个已经匹配好的完整括号,最后加上自身匹配好的完整括号,总共有4个已经完整匹配好的括号。

(为神魔感觉我的翻译这么辣鸡啊。。嘤嘤嘤)

思路:模拟大法好咯。看到数据量比较小,我就放心大胆的用数组模拟好了,左括号为0,右括号为1,完整括号为-1,读入的时候,模拟括号的排列顺序,最后进行统计,将统计的完整括号匹配的值存入一个新的数组,最后输出就好啦。

统计-1的总数除以2才是完整括号数。

#include<stdio.h>
#include<string.h>

int main()
{
    int str[50],num[50];
    int t,n,i,j,number,count,sum;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(str,0,sizeof(str));
        for(i = 0; i < n; i ++)
        {
            scanf("%d",&number);
            str[number+i] = 1;//右括号处赋值为1
        }
        sum = 0;//记录下标
        for(i = 0; i < 2*n; i ++)
        {
            count = 0;//统计完整括号数
            if(str[i]== 1)//遇到右括号时
            {
                for(j = i; j >= 0; j --)//从遇到的右括号处向左统计,共有多少个完整括号
                {
                    if(str[j] == -1)//遇到完整括号,总数加1
                        count ++;
                    if(str[j] == 0)//如果遇到与之匹配的左括号,结束循环
                        break;
                }
                count /=2;//总数除以2才是完整括号数
                str[i] = str[j] = -1;//将匹配好的左右括号标记为-1
                num[sum++] = count +1; //最后加上自身
            }
        }
        for(i = 0; i < n-1; i ++)
            printf("%d ",num[i]);
        printf("%d\n",num[n-1]);
    }
    return 0;
}
时间: 2024-07-30 23:56:07

poj1068 Parencodings【模拟】【刷题计划】的相关文章

BZOJ第一页刷题计划

BZOJ第一页刷题计划 已完成:1 / 100 BZOJ1000:A+B

刷题计划

我很后悔这一年来被我浪费过的每分每秒,我已经不想再浪费时间了. 平时不好好刷题还想着打比赛? 今年的目标就是刷完紫书第七章的搜索,第八章的贪心,第九章的dp,然后每次的cf补题尽量补到div2的DE题,如果时间有剩余,就学AC自动机等一些数据结构吧. 第一阶段:紫书ch7-ch9  时间11月末-12月31号 第二阶段  训练指南选择性的补充知识点,同时打一些5个小时比赛+补题  时间1月-2月 第三阶段  区域赛真题组队训练

刷题计划(转)

原文链接:http://my.oschina.net/lee24/blog/74949 初级: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和

[填坑][主线任务]历年NOIP刷题计划

今天又是喜闻乐见的非考试日,那么今天做点什么呢== 前些日子的主线任务陆陆续续(接近)完成了,好多蒙蔽的没学好的算法都算是入门补坑了 我听学长说,做题的顺序是:NOIP真题->NOIP模拟题->专项练习->杂题 啊哈!最重要的真题我还没做几道呢...于是这两天填填这个坑吧 [NOIP2016 Day1 T2]天天爱跑步 NOIP2016最难的题没有之一QAQ,原来做过一直没过,今天重新听讲解,总算打了出来 [NOIP2015 Day2 T3]运输计划 压轴题防AK,最后一个点及其凶残,并

HDU5697 刷题计划 dp+最小乘积生成树

分析:就是不断递归寻找靠近边界的最优解 学习博客(必须先看这个): 1:http://www.cnblogs.com/autsky-jadek/p/3959446.html 2:http://blog.csdn.net/u013849646/article/details/51524748 注:这里用的最小乘积生成树的思想,和dp结合 每次找满足条件的最优的点,只不过BZOJ裸题的满足条件是形成一棵树 这个题是大于m,生成树借用最小生成树进行求解最优,大于m用dp进行求解最优 #include

poj1753Flip Game【刷题计划】

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 47766   Accepted: 20383 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the

poj 2586 Y2K Accounting Bug【贪心】【刷题计划】

Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16154   Accepted: 8111 Description Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc

刷题计划(暑假)

暂定每天更新. 7.7 1. P1378 油滴扩展 2. P1306 斐波那契公约数 3. Bugs Integrated, Inc.(加深理解三进制dp) 4. UVa12206 Stammering Aliens(复习字符串hash) 5. P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 原文地址:https://www.cnblogs.com/wsmrxc/p/9275856.html

7、8月刷题总结

准备开学了囧,7.8月刷题记录,以后好来复习,并且还要好好总结! 数据结构: splay: [BZOJ]1503: [NOI2004]郁闷的出纳员(Splay) [BZOJ]1269: [AHOI2006]文本编辑器editor(Splay) [BZOJ]1507: [NOI2003]Editor(Splay) treap: [BZOJ]1862: [Zjoi2006]GameZ游戏排名系统 & 1056: [HAOI2008]排名系统(treap+非常小心) [BZOJ]3224: Tyvj