POJ 1068 Parencodings

题意:

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


P-sequence 4 5 6666
W-sequence 1 1 1456
p 序列: 右括号前面一共有几个左括号;
w 序列: 与该右括号括号匹配之后里面有几个左括号(第三个右括号与第三个括号匹配)

思路: 模拟

10998040 NY_lv10 1068 Accepted 224K 0MS C++ 875B 2012-11-08 20:30:59

 

View Code
 1 #include <iostream>

 2 #include <vector>

 3 using namespace std;

 4 

 5 vector<int> arr;

 6 vector<int> vec;

 7 

 8 int main()

 9 {

10     int t, n;

11     int x, tmpx, i, j;

12     int left, right;

13     cin>>t;

14     while (t--)

15     {

16         cin>>n;

17         vec.clear();

18         arr.clear();

19         tmpx = 0;

20         while (n--)

21         {

22             cin>>x;

23             for (i=0; i<x-tmpx; i++)

24                 arr.push_back(-1);  //左括号 "("

25             arr.push_back(1);    //右括号 ")"

26             tmpx = x;

27         }

28         left = right = 0;

29         

30         for (i=arr.size()-1; i>=0; i--)

31         {

32             if (arr[i] == 1)

33             {

34                 for (j=i; j>=0; j--)

35                 {

36                     if (arr[j] == 1)

37                     {

38                         left++;

39                         right++;

40                     }

41                     else

42                     {

43                         left--;

44                     }

45                     if (left == 0)

46                     {

47                         vec.push_back(right);

48                         right = 0;

49                         break;

50                     }

51                 }

52             }

53         }

54         cout<<vec[vec.size()-1];

55         for (i=vec.size()-2; i>=0; i--)

56             cout<<" "<<vec[i];

57         cout<<endl;

58     }

59     return 0;

60 }

 

 

你可能感兴趣的:(encoding)