zoukankan      html  css  js  c++  java
  • Parentheses Column Values

    Parentheses Column Values

    Between the columns using four parentheses ‘(‘, ‘)’, ‘[‘, ‘]’, a correct parentheses column is defined as below:

    1. ‘()’ or ‘[]’ consisting of a couple of parenthesis only is correct.
    2. If X is the correct parentheses column, both ‘(X)’ and ‘[X]’ are correct.
    3. If both X & Y are correct parentheses columns, the combined ‘XY’ is correct.

    For example, ‘(()[[]])’ or ‘(())[][]’ is the correct parentheses column but ‘([)]’ or ‘(()()[]’ is not. For a certain correct parentheses X, we define the value (in parentheses) in the row as below and indicate as Value(X).

    1. The value of ‘()’ parentheses column is 2.
    2. The value of ‘[]’ parentheses column is 3.
    3. The value of ‘(X)’ parentheses column is 2×Value(X).
    4. The value of ‘[X]’ parentheses is 3×Value(X).
    5. The value of ‘XY’ parentheses is Value(X)+Value(Y).

    For example, to find the parentheses value of ‘(()[[]])([])’, the value of parentheses ‘()[[]]’ is 2+3×3=11. Therefore, the parentheses value of ‘(()[[]])’ is 2×11=22. In addition, as the parentheses value of ‘([])’ is 2×3=6, the total parentheses column value is 22+6=28.
    Calculate parentheses values in given parentheses columns.

    Time limit: 1 second (java: 2 seconds)

    [Input]
    Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T (T ≤ 30) are given in a row.
    Character columns indicating parentheses columns are given on the first row per each test case. The length is over 1 below 30.

    [Output]
    Output the parentheses values on the first row of each test case. At this time, if parentheses columns are not correct, output zero.

    [I/O Example]
    Input

    2
    (()[[]])([])
    [][]((])

    Output
    Case #1  

    28  

    Case #2 
    0 

    代码:

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    
    using namespace std;
    int result = 0;
    char chars[30];
    int num[30];
    int st[30];
    int po;
    void pushSt(int v);
    int popSt();
    int emptySt();
    int beforeP(int v);
    int endP(int v);
    int matchP(int v);
    
    int main()
    {
        //freopen("input.txt","r",stdin);
        int N;
        cin >> N;
    
        int flag,correct,ch,ch1,ch2,match;
        for(int t=1; t<=N; t++)
        {
            scanf("%s",chars);
            int s = strlen(chars);
            //cout<<s<<endl;
            for(int i=0; i<s; i++)
            {
                if(chars[i] == '(')
                {
                    num[i] = -1;
                }
                else if(chars[i] == ')')
                {
                    num[i] = -2;
                }
                else if(chars[i] == '[')
                {
                    num[i] = -3;
                }
                else if(chars[i] == ']')
                {
                    num[i] = -4;
                }
            }
            result = 0;
            po = flag = 0;
            correct = 1;
            if(endP(num[0]))
            {
                correct = 0;
            }
            else
            {
                pushSt(num[0]);
            }
            for(int i=1; i<s; i++)
            {
                if(beforeP(num[i]))
                {
                    pushSt(num[i]);
                }
                else
                {
                    match = matchP(num[i]);
                    ch1 = 1;
    
                    while(!emptySt() && ((ch = popSt()) != match))
                    {
                        if(beforeP(ch))
                        {
                            correct = 0;
                            break;
                        }
                        if(flag == 0)
                        {
                            flag = 1;
                            ch1 = ch;
                        }
                        else{
                            ch2 = ch;
                            ch = ch1 + ch2;
                            pushSt(ch);
                            flag = 0;
                            ch1 = 1;
                        }
                    }
                    if(ch == match)
                    {
                        flag = 0;
                        if(num[i] == -2)
                        {
                            ch = ch1 * 2;
                            pushSt(ch);
                        }
                        else
                        {
                            ch = ch1 * 3;
                            pushSt(ch);
                        }
                    }
                    else
                    {
                        correct = 0;
                        break;
                    }
                }
    
            }
        ch = 0;
        if(correct == 1)
        {
            for(int i=0; i<po; i++)
            {
                if(beforeP(st[i]))
                {
                    correct = 0;
                    break;
                }
                else
                {
                    ch += st[i];
                }
            }
        }
        if(correct)
        {
            result = ch;
        }
        cout<<"Case #"<<t<<endl;
        cout<<ch<<endl;
        }
    
        //cout << "Hello world!" << endl;
        return 0;
    }
    
    void pushSt(int v)
    {
        st[po] = v;
        po++;
    }
    int popSt()
    {
        int t = st[po-1];
        po--;
        return t;
    }
    int emptySt()
    {
        if(po == 0)
            return 1;
        else
            return 0;
    }
    int beforeP(int v)
    {
        if(v == -1 || v == -3)
        {
            return 1;
        }
        else
            return 0;
    }
    int endP(int v)
    {
        if(v == -2 || v == -4)
            return 1;
        else
            return 0;
    }
    int matchP(int v)
    {
        if(v == -2)
            return -1;
        else if(v == -4)
            return -3;
    }
    View Code
  • 相关阅读:
    字符串匹配之朴素匹配
    XSS的攻击原理
    使用metasploit收集邮箱
    C++实现折半插入排序
    C++插入排序实现
    Java中的NIO
    Hashtable和HashMap区别(面试)
    面向对象:封装(一):构造函数;类的主方法;权限修饰符;对象的创建
    switch多分支语句
    递归和字母数字生成随机数
  • 原文地址:https://www.cnblogs.com/kingshow123/p/Cpractice.html
Copyright © 2011-2022 走看看