当前位置:网站首页>【练习-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)的题非要搞这些花里胡哨……是我学艺不精!
思想:
①用栈来存"(" 和 “[”
②如果碰到")" 要看栈首是不是"(", 如果是"[“或者栈为空,则是一个不合法的串。
③如果碰到”]" 要看栈首是不是"[", 如果是"("或者栈为空,则是一个不合法的串。
思想是这个样子,代码如何去写就要靠大家自己了!
、
边栏推荐
- ucorelab3
- 【练习-9】Zombie’s Treasure Chest
- Matlab comprehensive exercise: application in signal and system
- China's earthwork equipment market trend report, technical dynamic innovation and market forecast
- nodejs爬虫
- Eslint--- error: newline required at end of file but not found (EOL last) solution
- Research Report on market supply and demand and strategy of China's land incineration plant industry
- Cost accounting [15]
- China's PCB connector market trend report, technological innovation and market forecast
- MATLAB综合练习:信号与系统中的应用
猜你喜欢
用C语言写网页游戏
STM32学习记录:玩转按键控制蜂鸣器和LED
Learning record: Tim - capacitive key detection
JS --- all knowledge of JS objects and built-in objects (III)
Learning records: serial communication and solutions to errors encountered
MATLAB综合练习:信号与系统中的应用
毕业才知道IT专业大学生毕业前必做的1010件事
学习记录:使用STM32外部输入中断
Flex --- detailed explanation of flex layout attributes
ucorelab3
随机推荐
ucore lab7
Cost accounting [13]
【高老师UML软件建模基础】20级云班课习题答案合集
Cost accounting [14]
差分(一维,二维,三维) 蓝桥杯三体攻击
Research Report on market supply and demand and strategy of geosynthetics industry in China
ucorelab3
Interesting drink
信息安全-安全专业名称|CVE|RCE|POC|VUL|0DAY
TCP的三次握手与四次挥手
Research Report on surgical fluid treatment industry - market status analysis and development prospect prediction
MATLAB实例:阶跃函数的两种表达方式
Research Report on market supply and demand and strategy of China's Medical Automation Industry
China's peripheral catheter market trend report, technological innovation and market forecast
D - Function(HDU - 6546)女生赛
STM32 learning record: input capture application
E. Breaking the Wall
Research Report on market supply and demand and strategy of China's land incineration plant industry
Learning records: serial communication and solutions to errors encountered
Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction