poj1068 模拟

Parencodings

Time Limit: 1000 MS Memory Limit: 10000 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stack>

using namespace std;

int main()
{
    int t,n,temp;
    int a[1000];
    int val[1000],w[1000];
    cin>>t;
    while(t--)
    {
        memset(val,0,sizeof(val));
        cin>>n;
        for(int j=0; j<n; j++)
        {
            cin>>a[j];
            temp=a[j]+j;
            val[temp]=1;  ///让)==1 (==0
            /*for(int i=0;i<n*2;i++) ///输出1的地方
            cout<<val[i]<<‘ ‘;
            cout<<endl;*/
            ///cout<<val[temp]<<endl;   ==1    从后往前数)的个数
            for(int i=temp-1,sum=1,falg=0; i>=0; i--) ///  val[i]==) flag标记(是否与)匹配的
            {
                if(falg==0&&val[i]==0)   ///eg:)
                {
                    w[j]=sum;
                    break;
                }
                else if(val[i]==0)   ///eg:((())
                {
                    falg--;
                }
                else if(val[i]==1) ///eg:)))
                {
                    sum++;
                    falg++;
                }
            }
        }
        for(int i=0; i<n; i++)
        {
            cout<<w[i]<<‘ ‘;
        }
      cout<<endl;
    }
    return 0;
}

注释:

题意:P:从左到右  遇见第i个完整括号开始  (的个数     q:从左到右遇见第一个)时   与该)匹配的括号内 )的个数    (包含该))

1: 因为不用输出括号  所以将(标记为0  )标记为1

2:对于q 要计算一个大括号内的全部)   所以要从后向前遍历

poj1068 模拟

时间: 2024-08-24 14:30:38

poj1068 模拟的相关文章

poj1068解题报告(模拟类)

POJ 1068,题目链接http://poj.org/problem?id=1068 题意: 对于给出给出的原括号串S,对应两种数字密码串P.W: S         (((()()()))) P-sequence      4 5 6666   (Pi表示第i个右括号前面有多少个左括号) W-sequence     1 1 1456    (Wi表示第i个右括号对应它前面的第几个左括号) 要求给出P串,求W. 思路: 1. 模拟类题型.将输入的P串先装换为S串,再由S串得到W串. 2. 左

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

poj1068题解报告与感想

poj1068描述了这样一个问题:给出一种括号序列的表示形式名叫P序列,规则是统计出每个右括号之前的左括号个数作为序列每项的值.然后要求你根据这个求括号列的W序列值,W序列的规则是统计每一个右括号和与其匹配的左括号之间所有匹配后的括号个数. 刚看到这个题的时候,想法是十分简单的,就是模拟.我先用给的P序列推出括号序列,然后再去求W序列.后来我在discuss区发现一种十分不错的做法,贴在这里. 假定输入的P序列存在数组p中,则p[i]表示第i个P序列元素. 根据P序列的定义,每个数值表示的是当前

CentOS系统启动及内核大破坏模拟实验

讲过了centos的启动流程,此时是不是想来点破坏呢?那就尽情的玩耍吧,记得在实验之前拍个快照,万一哪个环节错误恢复不回来了呢,毕竟数据无价,话不多说,开始. 一.删除伪系统根.(ramdisk文件) (1)模拟误操作删除ramdisk文件. ①模拟误删除initramfs-3.10.0-514.el7.x86_64.img文件. ②为当前正在使用的内核重新制作ramdisk文件 格式为:mkinitrd /boot/initramfs-$(uname -r).img $(uname -r) (

NYOJ 2356: 哈希计划【模拟】

题目描述 众所周知,LLM的算法之所以菜,就是因为成天打游戏,最近LLM突然想玩<金庸群侠传X>,结果进去后各种被虐,LLM就开始研究这个游戏的代码,顺便还学会了一点点点点lua语言,然后就开始了伟大的改游戏代码之旅,然后LLM发现自己too young了,这个游戏把所有的文本都进行了哈希,如果自己改了代码或者剧情文本的话它哈希出来的值就会和原来的哈希值不一样......然后游戏就会打不开.....,现在LLM发现了文本的哈希函数,要求你写个程序,功能为: 输入一段字符串,输出一个哈希值 为了

爬虫——模拟点击动态页面

动态页面的模拟点击: 以斗鱼直播为例:http://www.douyu.com/directory/all 爬取每页的房间名.直播类型.主播名称.在线人数等数据,然后模拟点击下一页,继续爬取 #!/usr/bin/python3 # -*- conding:utf-8 -*- __author__ = 'mayi' """ 动态页面的模拟点击: 模拟点击斗鱼直播:http://www.douyu.com/directory/all 爬取每页房间名.直播类型.主播名称.在线人数

爬虫——网站模拟登录

使用Selenium与PhantomJS模拟登录豆瓣:https://www.douban.com/ #!/usr/bin/python3 # -*- conding:utf-8 -*- __author__ = 'mayi' """ 模拟登录豆瓣:https://www.douban.com/ """ from selenium import webdriver # 调用环境变量指定的PhantomJS浏览器创建浏览器对象,executable

python爬虫 模拟登陆校园网-初级

最近跟同学学习爬虫的时候看到网上有个帖子,好像是山大校园网不稳定,用py做了个模拟登陆很有趣,于是我走上了一条不归路..... 先上一张校园网截图 首先弄清一下模拟登陆的原理: 1:服务器判定浏览器登录使用浏览器标识,需要模拟登陆 2: 需要post账号,密码,以及学校id python走起,我用的2.7版本,用notepad++写的,绑定python可以直接运行 由于是模拟网页登陆,需要导入urllib urllib2 cookielib库,前两个有与网页直接的接口,cookielib就是用来

Android模拟位置信息

Android模拟位置程序,俗称GPS欺骗,只能修改采用GPS定位的软件. 手机定位方式目前有4种:基站定位,WIFI定位,GPS定位,AGPS定位 常见的修改手法: 1. 抓包欺骗法,抓包改包欺骗服务器端, 但是得专门去针对某款app,而且现在很多app数据包都加密了 2. hook java层经纬度获取函数, 这个方法以前可以用,现在不行了 3. hook native层经纬度获取函数 4. 使用允许模拟地址位置信息(不是很通用有版本限制) 为了修改微信朋友圈地理位置信息,为了好玩 试过了上