Day 2 A - The Balance of the World

Day 2 A - The Balance of the World

题目正文

The world should be finely balanced. Positive vs. negative, light vs. shadow, and left vs. right brackets. Your mission is to write a program that judges whether a string is balanced with respect to brackets so that we can observe the balance of the world.

A string that will be given to the program may have two kinds of brackets, round (“( )”) and square (“[ ]”). A string is balanced if and only if the following conditions hold.

For every left round bracket (“(”), there is a corresponding right round bracket (“)”) in the following part of the string.
For every left square bracket (“[”), there is a corresponding right square bracket (“]”) in the following part of the string.
For every right bracket, there is a left bracket corresponding to it.
Correspondences of brackets have to be one to one, that is, a single bracket never corresponds to two or more brackets.
For every pair of corresponding left and right brackets, the substring between them is balanced.
Input
The input consists of one or more lines, each of which being a dataset. A dataset is a string that consists of English alphabets, space characters, and two kinds of brackets, round (“( )”) and square (“[ ]”), terminated by a period. You can assume that every line has 100 characters or less. The line formed by a single period indicates the end of the input, which is not a dataset.

Output
For each dataset, output “yes” if the string is balanced, or “no” otherwise, in a line. There may not be any extra characters in the output.

Sample Input
So when I die (the [first] I will see in (heaven) is a score list).
[ first in ] ( first out ).
Half Moon tonight (At least it is better than no Moon at all].
A rope may form )( a trail in a maze.
Help( I[m being held prisoner in a fortune cookie factory)].
([ (([( [ ] ) ( ) (( ))] )) ]).
.
.
Output for the Sample Input
yes
yes
no
no
no
yes
yes

翻译

世界应该保持良好的平衡。正括号与负括号、光括号与影括号、左括号与右括号。您的任务是编写一个程序,判断字符串是否相对于括号平衡,以便我们可以观察世界的平衡。

将提供给程序的字符串可能有两种括号,圆括号(())和方括号([])。当且仅当下列条件成立时,字符串才是平衡的。

对于每个左圆括号(“”),字符串的以下部分中都有一个相应的右圆括号(“”)。

对于每个左方括号(“[”),字符串的以下部分中都有一个相应的右方括号(“])。

每个右括号对应一个左括号。

括号的对应关系必须是一对一,也就是说,一个括号永远不会对应两个或多个括号。

对于每对相应的左括号和右括号,它们之间的子字符串是平衡的。

输入

输入由一行或多行组成,每行都是一个数据集。数据集是一个字符串,由英文字母、空格字符和两种括号组成,圆括号(())和方括号([]),以句点结尾。您可以假设每行包含100个或更少的字符。由单个句点组成的行表示输入的结束,该输入不是数据集。

输出

对于每个数据集,如果字符串是平衡的,则输出“是”,否则在一行中输出“否”。输出中可能没有任何额外字符。

题意

主要是判断括号是否匹配

代码

`.

//1.定义一个字符串
//2.一个死循环while(true)输入字符串,判断如果等于点返回0直接结束程序
//3.定义一个标记,一个栈char型
//4.定义一个for循环,加入标记
//5.if语句判断等于左括号或者时左中括号进栈
//6.反之,如果等于右括号,出现两种情况,如果栈为空或者栈顶不是左括号,为错误的情况,否则出栈
//7.如果是右中括号,同6
//8.最后判断栈是否为空,不为空为错误的情况
//9.输出yes,no
#include<stdio.h>
#include<iostream>
#include<stack>
using namespace std;
int main()
{
    string s;
    while(true)
    {
        getline(cin,s);
        if(s==".")return 0;
        bool flag=true;
        stack<char> st;
        for(int i=0; flag && i<s.length();++i)
        {
            if(s[i]=='('||s[i]=='[')
            {
                st.push(s[i]);
            }
            else if(s[i]==')')
            {
                if(st.empty()||st.top()!='(')flag=false;
                else
                    st.pop();
            }
             else if(s[i]==']')
            {
                if(st.empty()||st.top()!='[')flag=false;
                else
                    st.pop();
            }
        }
    if(!st.empty())flag=false;
    if(flag)
        cout<<"yes"<<endl;
    else
        cout<<"no"<<endl;
    }

}

总结

主要是运用栈,进行括号的匹配,其实来说不难。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值