码迷,mamicode.com
首页 > 其他好文 > 详细

poj1068解题报告(模拟类)

时间:2014-06-02 01:21:09      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   a   

POJ 1068,题目链接http://poj.org/problem?id=1068

题意:

对于给出给出的原括号串S,对应两种数字密码串PW

S         (((()()()))) P-sequence      4 5 6666   (Pi表示第i个右括号前面有多少个左括号) W-sequence     1 1 1456    (Wi表示第i个右括号对应它前面的第几个左括号)

要求给出P串,求W

思路:

1. 模拟类题型。将输入的P串先装换为S串,再由S串得到W串。

2. 左右括号可以用truefalse表示。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//356K  0MS
#include <cstdio>
 
#define LEFT true
#define RIGHT false
 
bool s_data[40];//最多20个括号
int main()
{
    int caseNum, oneCount;
    int temp, last;
    scanf("%d", &caseNum);
    do
    {
        scanf("%d", &oneCount);
 
        int S_Len = 0; //parenthesesNum * 2
 
        //1. get P-string , and convert to S
        scanf("%d", &temp);//P - first
        for (int i=0; i<temp; ++i) s_data[S_Len++] = LEFT;
        s_data[S_Len++] = RIGHT;
        //P  second -- count
        for (int i=1; i<oneCount; ++i)
        {
            last = temp;
            scanf("%d", &temp);
            for (int idx=0; idx<temp-last; ++idx) s_data[S_Len++] = LEFT;
            s_data[S_Len++] = RIGHT;
        }
        //2. convet S to W-string
        int pPos = 0;
        for (int idx=0; idx < S_Len; ++idx)
        {
            if (s_data[idx] == RIGHT)
            {
                int val=1, ret=1;
                pPos = idx;
                while(pPos-- > 0)
                {
                    if (s_data[pPos] == RIGHT){
                        ++val; ++ret;
                    }else {
                        --val;
                        if (val == 0){ //匹配成功
                            printf("%d ", ret);
                            break;
                        }
                    }
                }
            }
        }
        printf("\n");
    } while (--caseNum);
 
    return 0;
}

 

 

 

poj1068解题报告(模拟类),布布扣,bubuko.com

poj1068解题报告(模拟类)

标签:c   style   class   blog   code   a   

原文地址:http://www.cnblogs.com/songcf/p/3763653.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!