Parentheses Balance(stack)


Parentheses Balance


You are given a string consisting of parentheses () and []. A string of this type is said to be correct:(a) if it is the empty string(b) if A and B are correct, AB is correct,(c) if A is correct, (A) and [A] is correct.Write a program that takes a sequence of strings of this type and check their correctness. Yourprogram can assume that the maximum string length is 128.InputThe file contains a positive integer n and a sequence of n strings of parentheses ‘()’ and ‘[]’, one stringa line.OutputA sequence of ‘Yes’ or ‘No’ on the output file.

Sample Input












using namespace std;

bool judge(char c1,char c2)
    if(c1=='('&&c2==')')return 1;
    if(c1=='['&&c2==']')return 1;
    return 0;
int main()
    int T;
    char str[150];
        stack<char> s;
        int leap=1,flag=0,cntl=0,cntr=0;
        int len = strlen(str);
        for(int i=0;i<len;++i){
            if(str[i]=='('||str[i]=='[')    cntl++;
            else cntr++;
            if(cntr>cntl) {flag=1;break;} //判断类似:))((
        for(int i=0;i<len;++i){
            else if(!s.empty()){

        if(leap)  cout<<"Yes"<<endl;
        else      cout<<"No"<<endl;
    return 0;


The Balance


DescriptionnnMs. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. For example, to measure 200mg of aspirin using 300mg weights and 700mg weights, she can put one 700mg weight on the side of the medicine and three 300mg weights on the opposite side (Figure 1). Although she could put four 300mg weights on the medicine side and two 700mg weights on the other (Figure 2), she would not choose this solution because it is less convenient to use more weights. nYou are asked to help her by calculating how many weights are required. nn![]( input is a sequence of datasets. A dataset is a line containing three positive integers a, b, and d separated by a space. The following relations hold: a != b, a <= 10000, b <= 10000, and d <= 50000. You may assume that it is possible to measure d mg using a combination of a mg and b mg weights. In other words, you need not consider "no solution" cases. nThe end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.nOutputnnThe output should be composed of lines, each corresponding to an input dataset (a, b, d). An output line should contain two nonnegative integers x and y separated by a space. They should satisfy the following three conditions. nYou can measure dmg using x many amg weights and y many bmg weights. nThe total number of weights (x + y) is the smallest among those pairs of nonnegative integers satisfying the previous condition. nThe total mass of weights (ax + by) is the smallest among those pairs of nonnegative integers satisfying the previous two conditions.nnNo extra characters (e.g. extra spaces) should appear in the output.nSample Inputnn700 300 200n500 200 300n500 200 500n275 110 330n275 110 385n648 375 4002n3 1 10000n0 0 0nSample Outputnn1 3n1 1n1 0n0 3n1 1n49 74n3333 1



DescriptionnnGigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance. nIt orders two arms of negligible weight and each arm's length is 15. Some hooks are attached to these arms and Gigel wants to hang up some weights from his collection of G weights (1 <= G <= 20) knowing that these weights have distinct values in the range 1..25. Gigel may droop any weight of any hook but he is forced to use all the weights. nFinally, Gigel managed to balance the device using the experience he gained at the National Olympiad in Informatics. Now he would like to know in how many ways the device can be balanced. nnKnowing the repartition of the hooks and the set of the weights write a program that calculates the number of possibilities to balance the device. nIt is guaranteed that will exist at least one solution for each test case at the evaluation. nInputnnThe input has the following structure: n• the first line contains the number C (2 <= C <= 20) and the number G (2 <= G <= 20); n• the next line contains C integer numbers (these numbers are also distinct and sorted in ascending order) in the range -15..15 representing the repartition of the hooks; each number represents the position relative to the center of the balance on the X axis (when no weights are attached the device is balanced and lined up to the X axis; the absolute value of the distances represents the distance between the hook and the balance center and the sign of the numbers determines the arm of the balance to which the hook is attached: '-' for the left arm and '+' for the right arm); n• on the next line there are G natural, distinct and sorted in ascending order numbers in the range 1..25 representing the weights' values. nOutputnnThe output contains the number M representing the number of possibilities to poise the balance.nSample Inputnn2 4 n-2 3 n3 4 5 8nSample Outputnn2