poj 1068 Parencodings(模拟)

Parencodings

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19434   Accepted: 11734

题目大意:  给出一个整数表示有n组测试样例  每个样例包含两组数  一个整数m 和一个有m个元素的p数组

数组中的元素表示的意思为第i个‘ ) ‘之前有多少个‘ ( ‘   。

要求输出一个w数组其元素表示的意思为在第i个’ ) ‘和它匹配的’(‘之间有多少组已匹配好的括号  (包括其自身)

例如

S		(((()()())))

	P-sequence	    4 5 6666

	W-sequence	    1 1 1456

根据题目意思只要依据p数组模拟出该括号字符串  再将w数组求出来即可

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    int a[25],ans[25],n,m,i,j;
    char mm[25];
    cin>>n;
    while(n--)
    {
        cin>>m;
        a[0]=0;
        for(i=1;i<=m;i++)
            cin>>a[i];
            for(i=1;i<=m;i++)
                for(j=a[i-1]+i;j<=a[i]+i;j++)
            {

                if(j==a[i]+i)
                  {
                      mm[j]=')';//cout<<j<<" "<<mm[j];
                  }
                else
                  {
                      mm[j]='(';//cout<<j<<" "<<mm[j];
                  }
            }
            int l=0;
            int s=0;
            for(i=1;i<j;i++)
                if(mm[i]==')')
                {
                    int sum=1;
                   for(int k=i-1;k>0;k--)
                       {
                           if(mm[k]==')')
                               {
                                   s++;
                                   sum++;
                                }
                            else if(mm[k]=='('&&s)
                                {
                                    s--;
                                }
                            else if(mm[k]=='('&&!s)
                                 {
                                    ans[l]=sum;//cout<<k<<endl;
                                    l++;
                                    break;
                                 }
                       }
                }
            for(i=0;i<l;i++)
            {
                if(i==0)
                cout<<ans[i];
                else cout<<" "<<ans[i];
            }
            cout<<endl;

    }

    return 0;
}
时间: 2024-08-24 14:30:39

poj 1068 Parencodings(模拟)的相关文章

[ACM] POJ 1068 Parencodings(模拟)

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19352   Accepted: 11675 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

poj 1068 Parencodings 模拟题

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 B

poj 1068 Parencodings

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22797   Accepted: 13363 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

HDU 1361 &amp; POJ 1068 Parencodings(模拟)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1361 POJ:http://poj.org/problem?id=1068 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

POJ 1068 Parencodings (模拟),暑假第一题~

终于考完高线大,头也不那么疼了,也终于有空来刷题了~,先来一发水题试试水 题目链接:http://poj.org/problem?id=1068 简单的模拟,麻烦在理解题意,,英语好的请无视这句: [分析] S (((()()()))) P-sequence 4 5 6666 W-sequence 1 1 1456 如这个 S字符串, 题目给你一串数 4 5 6 6 6 6    对应P1 P2,,,Pn 意思是,第n个右括号')'左边有Pn 个左括号 ,,例如 456666 第1个右括号")&

POJ 之 Parencodings (类似括号的处理问题)

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19550   Accepted: 11804 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

【POJ 1028】模拟浏览器

本题要求模拟浏览器的前进.后退等操作. 用两个栈实现,一个控制前进,一个控制后退. 在前进与后退操作中,从一个栈中弹出栈顶元素,压入另一个栈中. 当打开一个新网页时,将前进栈清空. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> #include

POJ 1835 大模拟

宇航员 #include<iostream> #include<cstdio> #include<string> #include<cstring> #define maxn 10010 using namespace std; int a[7],temp[7]; char str[10]; void solve(int str2[],int str3[]) { if(strcmp(str,"forward")==0)//方向不变 { s

POJ 1102 LC-Display 模拟

Description A friend of you has just bought a new computer. Until now, the most powerful computer he ever used has been a pocket calculator. Now, looking at his new computer, he is a bit disappointed, because he liked the LC-display of his calculator