当前位置:网站首页>【练习-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)的题非要搞这些花里胡哨……是我学艺不精!
思想:
①用栈来存"(" 和 “[”
②如果碰到")" 要看栈首是不是"(", 如果是"[“或者栈为空,则是一个不合法的串。
③如果碰到”]" 要看栈首是不是"[", 如果是"("或者栈为空,则是一个不合法的串。
思想是这个样子,代码如何去写就要靠大家自己了!
、
边栏推荐
- Es6--- two methods of capturing promise status as failed
- CS zero foundation introductory learning record
- ucore lab 2
- Opencv learning log 32 edge extraction
- Market trend report, technical innovation and market forecast of Chinese hospital respiratory humidification equipment
- 0-1 knapsack problem (I)
- 0 - 1 problème de sac à dos (1)
- F - Birthday Cake(山东省赛)
- Flink 使用之 CEP
- 动态规划前路径问题优化方式
猜你喜欢
随机推荐
Learning record: understand systick system timer and write delay function
E. Breaking the Wall
学习记录:USART—串口通讯
FSM and I2C experiment report
nodejs爬虫
Ball Dropping
China medical check valve market trend report, technical dynamic innovation and market forecast
Flex --- detailed explanation of flex layout attributes
STM32 learning record: input capture application
Opencv learning log 15 count the number of solder joints and output
Shell脚本编程
Research Report on medical toilet industry - market status analysis and development prospect forecast
Nodejs+vue网上鲜花店销售信息系统express+mysql
China's peripheral catheter market trend report, technological innovation and market forecast
ucore lab5
C语言是低级和高级的分水岭
Research Report on market supply and demand and strategy of China's land incineration plant industry
Research Report of peripheral venous catheter (pivc) industry - market status analysis and development prospect prediction
Research Report on shell heater industry - market status analysis and development prospect forecast
Cost accounting [13]