POJ1068 Parencodings(模拟)

Parencodings

题目链接:

http://poj.org/problem?id=1068

解题思路:

规则就是:每个右括号之前的左括号数序列为P=4 5 6 6 6 6,而每个右括号所在的括号内包含的括号数为W=1 1 1 4 5 6。简单模拟即可。

wrong了很多次,如果你用数组保存,就把数组开到100以上,最好不用。

AC代码(没用数组保存):

#include <iostream>
#include <cstdio>
using namespace std;

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n,ch[100],x,t1 = 0,t2;
        int i,j,ans = 0;
        scanf("%d",&n);
        for(i = 0; i < n; i++){
            scanf("%d",&x);
            t2 = x-t1;
            for(j = 0; j < t2; j++)
                ch[ans++] = 0;
            ch[ans++] = 1;
            t1 = x;
        }
        //cout<<ans<<endl;
        for(i = 0; i < ans; i++)
            //cout<<ch[i]<<endl;
        for(i = 0; i < ans; i++)
            if(ch[i] == 1){
                int sum = 1,t = 1;
                for(j = i-1;; j--){
                    if(ch[j] == 0)
                        t--;
                    if(ch[j] == 1){
                        t++;
                        sum++;
                    }
                    if(t == 0){
                        printf("%d ",sum);
                        break;
                    }
                    //cout<<sum<<endl;
                }
            }
        printf("\n");
    }
    return 0;
}


AC代码(用数组保存):

#include <iostream>
#include <cstdio>
using namespace std;

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n,p[100],w[100],ch[10000];
        int i,j,ans = 0;
        scanf("%d",&n);
        for(i = 0; i < n; i++)
            scanf("%d",&p[i]);
        for(i = 0; i < p[0]; i++)
            ch[ans++] = 0;
        ch[ans++] = 1;
        for(i = 1; i < n; i++){
            int t = p[i]-p[i-1];
            for(j = 0; j < t; j++)
                ch[ans++] = 0;
            ch[ans++] = 1;
        }
        //cout<<ans<<endl;
        for(i = 0; i < ans; i++)
            if(ch[i] == 1){
                int sum=1,t=1;
                for(j = i-1;; j--){
                    if(ch[j] == 0)
                        t--;
                    if(ch[j] == 1){
                        t++;
                        sum++;
                    }
                    if(t == 0){
                        printf("%d ",sum);
                        break;
                    }
                    //cout<<sum<<endl;
                }
            }
        printf("\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值