当前位置:网站首页>【练习-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)的题非要搞这些花里胡哨……是我学艺不精!
思想:
①用栈来存"(" 和 “[”
②如果碰到")" 要看栈首是不是"(", 如果是"[“或者栈为空,则是一个不合法的串。
③如果碰到”]" 要看栈首是不是"[", 如果是"("或者栈为空,则是一个不合法的串。
思想是这个样子,代码如何去写就要靠大家自己了!
、
边栏推荐
- Learning record: how to perform PWM output
- Truck History
- Medical colposcope Industry Research Report - market status analysis and development prospect forecast
- UCORE Lab 1 system software startup process
- Learning record: Tim - capacitive key detection
- 【练习-10】 Unread Messages(未读消息)
- 学习记录:如何进行PWM 输出
- Perinatal Software Industry Research Report - market status analysis and development prospect forecast
- Ball Dropping
- 信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
猜你喜欢
学习记录:使用STM32外部输入中断
信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
STM32 how to use stlink download program: light LED running light (Library version)
Stm32 dossiers d'apprentissage: saisie des applications
Ball Dropping
毕业才知道IT专业大学生毕业前必做的1010件事
学习记录:理解 SysTick系统定时器,编写延时函数
X-Forwarded-For详解、如何获取到客户端IP
ucore lab5
Learning record: USART serial communication
随机推荐
Record of brushing questions with force deduction -- complete knapsack problem (I)
Cost accounting [22]
ucore lab5
Accounting regulations and professional ethics [1]
Report on the market trend, technological innovation and market forecast of printing and decorative paper in China
Research Report on market supply and demand and strategy of China's earth drilling industry
初入Redis
China chart recorder market trend report, technology dynamic innovation and market forecast
学习记录:串口通信和遇到的错误解决方法
对iptables进行常规操作
学习记录:理解 SysTick系统定时器,编写延时函数
China's earthwork equipment market trend report, technical dynamic innovation and market forecast
Accounting regulations and professional ethics [3]
C语言学习笔记
Alice and Bob (2021牛客暑期多校训练营1)
China potato slicer market trend report, technical dynamic innovation and market forecast
学习记录:USART—串口通讯
0 - 1 problème de sac à dos (1)
Opencv learning log 33 Gaussian mean filtering
Learning record: how to perform PWM output