当前位置:网站首页>[exercise-1] (UVA 673) parentheses balance/ balanced brackets (stack)
[exercise-1] (UVA 673) parentheses balance/ balanced brackets (stack)
2022-07-06 15:56:00 【Flame car】
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
What do you mean :
Enter a include "()“ and ”[]" The bracket sequence of , Judge whether it is legal . The rules :
① Empty string method
② If A and B It's all legal , be AB legal
③ If A Legal principle (A) and [A] It's all legal
AC Code :
#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;
}
It's quite speechless , This question is too annoying …… Thought it was a water problem , I didn't expect it to be a dirty water problem , How annoying .
If you understand the meaning of the topic, it should not be very difficult ( Probably ), I found out when I wrote this blog Empty strings are also legal characters !!
No wonder you have to getline() Input , This is the input empty string .
But why do I have to write one before putchar() ??? Is not very good , If there is a big man, you can tell me (T^T).
These two points are simply disgusting, I guess , There is nothing to say about an exam stack (stack) I have to do all these fancy things …… I'm not good at learning !
thought :
① Use the stack to store "(" and “[”
② If you come across ")" It depends on whether the top of the stack is "(", If it is "[“ Or the stack is empty , Is an illegal string .
③ If you come across ”]" It depends on whether the top of the stack is "[", If it is "(" Or the stack is empty , Is an illegal string .
Thought is like this , How to write the code depends on you !
、
边栏推荐
- China exterior wall cladding (EWC) market trend report, technical dynamic innovation and market forecast
- Record of force deduction and question brushing
- Opencv learning log 18 Canny operator
- 对iptables进行常规操作
- Research Report on market supply and demand and strategy of China's medical chair industry
- Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
- 信息安全-威胁检测引擎-常见规则引擎底座性能比较
- 渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
- 【练习-6】(Uva 725)Division(除法)== 暴力
- Record of brushing questions with force deduction -- complete knapsack problem (I)
猜你喜欢

Learning record: how to perform PWM output

X-forwarded-for details, how to get the client IP

D - Function(HDU - 6546)女生赛

滲透測試 ( 1 ) --- 必備 工具、導航

Information security - threat detection - detailed design of NAT log access threat detection platform

渗透测试 ( 8 ) --- Burp Suite Pro 官方文档

Learning record: understand systick system timer and write delay function
![mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’](/img/e6/f4a696179282fe1f4193410c5a493a.png)
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’

Learning record: use STM32 external input interrupt

STM32学习记录:LED灯闪烁(寄存器版)
随机推荐
Printing quality inspection and verification system Industry Research Report - market status analysis and development prospect forecast
Research Report on market supply and demand and strategy of Chinese graphic screen printing equipment industry
China's earthwork equipment market trend report, technical dynamic innovation and market forecast
China's PCB connector market trend report, technological innovation and market forecast
渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
【练习-6】(PTA)分而治之
China earth moving machinery market trend report, technical dynamic innovation and market forecast
Borg Maze (BFS+最小生成树)(解题报告)
【练习4-1】Cake Distribution(分配蛋糕)
信息安全-安全编排自动化与响应 (SOAR) 技术解析
Opencv learning log 16 paperclip count
Research Report of pharmaceutical solvent industry - market status analysis and development prospect prediction
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
Cost accounting [14]
Cost accounting [14]
Accounting regulations and professional ethics [2]
【练习-10】 Unread Messages(未读消息)
用C语言写网页游戏
Cost accounting [21]
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志