zoukankan      html  css  js  c++  java
  • POJ 1068 Parencodings(模拟)

    题目链接:https://vjudge.net/problem/POJ-1068

    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

    题目大意:对于一个合法括号序列s,p序列表示每一个右括号左边有几个左括号,w序列表示与右括号匹配的是第几个左括号。先给出p序列要求w序列。

    /*
    POJ 1068 Parencodings
    简单模拟
    用数组a[i]表示第i个右括号和第i+1个右括号间的左括号数
    然后逐渐找和右括号匹配的左括号所处的位置
    i-j
    
    */
    
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    const int N=100;
    int t,n,p[N],w[N],a[N];
    
    int main()
    {
    
        cin>>t;
        while(t--)
        {
            cin>>n;
            for(int i=1;i<=n;i++) cin>>p[i];
            a[0]=p[1];
            for(int i=1;i<n;i++){
                a[i]=p[i+1]-p[i];
            }
            int j;
            for(int i=1;i<=n;i++)
            {
                for(j=i-1;j>=0;j--)//匹配右括号
                {
                    if(a[j]>0)
                    {
                        a[j]--;
                        break;
                    }
                }
                w[i]=i-j;
            }
            for(int i=1;i<n;i++) cout<<w[i]<<" ";
            cout<<w[n]<<endl;
        }
        return 0;
    }
  • 相关阅读:
    批量执行工具PSSH详解
    详解IPTABLES
    nginx启动脚本
    ansible离线安装
    Linux性能评估工具
    Python中路径操作
    mongodb Enable Auth
    MySQL配置参数说明
    redis未授权访问
    php反序列化笔记
  • 原文地址:https://www.cnblogs.com/Fy1999/p/9363596.html
Copyright © 2011-2022 走看看