当前位置:网站首页>【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
2022-07-06 09:26:00 【火焰车】
A . Parentheses Balance
Description
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. Your program can assume that the maximum string length is 128.
Input
The file contains a positive integer n and a sequence of n strings of parentheses ‘()’ and ‘[]’, one string a line.
Output
A sequence of ‘Yes’ or ‘No’ on the output file.
Samples
Input
3
([])
(([()])))
([()])()
Output
Yes
No
Yes
大致意思:
输入一个包含"()“和”[]"的括号序列,判断是否合法。规则:
①空串合法
②如果A和B都合法,则AB合法
③如果A合法则(A)和[A]都合法
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
int n;
int main()
{
cin >> n;
getchar();
while(n--){
string s;
stack<char>st;
getline(cin,s);
int len = s.size();
for(int i = 0; i < len; i++)
{
if(st.empty())
st.push(s[i]);
else
{
if((s[i] == ')' && st.top() == '(') || (s[i] == ']' && st.top() == '['))
st.pop();
else
st.push(s[i]);
}
}
if(st.empty())
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
就挺无语的,这个题太烦人了……以为是个水题,没想到是个脏人的水题,好烦啊。
如果懂了题意应该不是很难(大概),写这篇博客的时候才发现空串也是合法字符!!
怪不得还必须得 getline() 输入,这个就是输入空串的。
但是为啥前面还非要写一个 putchar() ???不是很懂,如果有大佬可以告诉我一下(T^T)。
这两个点就是单纯恶心人的吧估计,没啥好说的一个考栈(stack)的题非要搞这些花里胡哨……是我学艺不精!
思想:
①用栈来存"(" 和 “[”
②如果碰到")" 要看栈首是不是"(", 如果是"[“或者栈为空,则是一个不合法的串。
③如果碰到”]" 要看栈首是不是"[", 如果是"("或者栈为空,则是一个不合法的串。
思想是这个样子,代码如何去写就要靠大家自己了!
、
边栏推荐
- China's PCB connector market trend report, technological innovation and market forecast
- B - 代码派对(女生赛)
- 基于web的照片数码冲印网站
- Eslint--- error: newline required at end of file but not found (EOL last) solution
- Interesting drink
- STM32 learning record: input capture application
- STM32 learning record: LED light flashes (register version)
- JS --- detailed explanation of JS DOM (IV)
- MATLAB综合练习:信号与系统中的应用
- 信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
猜你喜欢
随机推荐
E. Breaking the Wall
Cost accounting [14]
JS --- detailed explanation of JS DOM (IV)
Learning record: Tim - capacitive key detection
STM32 learning record: LED light flashes (register version)
Research Report on market supply and demand and strategy of Chinese graphic screen printing equipment industry
Research Report on surgical fluid treatment industry - market status analysis and development prospect prediction
Es6---es6 content details
Cost accounting [19]
VS2019初步使用
数据在内存中的存储&载入内存,让程序运行起来
Opencv learning log 19 skin grinding
Medical colposcope Industry Research Report - market status analysis and development prospect forecast
0-1背包問題(一)
Accounting regulations and professional ethics [1]
Cost accounting [23]
csapp shell lab
Opencv learning log 31 -- background difference
信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
Market trend report, technical innovation and market forecast of Chinese hospital respiratory humidification equipment