热冗余冷冗余_冗余支架

热冗余冷冗余

Problem statement:

问题陈述:

Given a string of balanced expression, find if it contains a redundant parenthesis or not. A set of parentheses is redundant if the same sub-expression is surrounded by unnecessary or multiple brackets. Print "Yes" if redundant else "No".

给定一串平衡表达式,请查找它是否包含多余的括号。 如果相同的子表达式被不必要的或多个括号括起来,则一组括号是多余的。 如果多余则打印“是”,否则打印“否”。

Input:

输入:

The first line of input contains an integer T denoting the number of test cases. The next line T contains an expression. The expression contains all characters and ^, *, /, +, -.

输入的第一行包含一个整数T,表示测试用例的数量。 下一行T包含一个表达式。 该表达式包含所有字符以及^,*,/,+,-

Output:

输出:

For each test case, in a new line, print YES or NO if the expression is redundant or not.

对于每个测试用例,如果表达式是否多余,请在新行中打印YES或NO。

Examples:

例子:

    Input:	
T = 1
((a+b))
Output: 
YES
(a+b) is surrounded by extra (), which is of no need.
Input:
T = 1
(a+(a+b))
Output: 
NO
here there is no extra bracket.

Solution Approach

解决方法

Stack Approach

堆叠方式

We will traverse from left to right and perform the following operations.

我们将从左到右遍历并执行以下操作。

  • If the given character is not ')' then push that into stack otherwise we check for other probabilities as:

    如果给定字符不是')',则将其压入堆栈,否则我们将检查其他概率为:

  • We start to pop elements from the stack and check if the immediately popped element is '(' without any other any operator (+, -, /, *) in between them then it is a possible case of redundant brackets:

    我们开始从堆栈中弹出元素,并检查立即弹出的元素之间是否没有其他任何运算符(+,-,/,*)为'(' ,那么可能是多余的括号:

  • If the immediately popped element is open bracket ')' then it is a condition of the redundant bracket.

    如果立即弹出的元素是方括号')',则这是冗余方括号的条件。

Example:

例:

    ((a)),(((a+b))),((c)+d)

Pseudo Code:

伪代码:

string Redundant(string str)
{
stack<char>st     //declare stack
//iterate from left to right
for(int i=0;i<str.length();i++)
{
//is character is not ')' then push it into the stack.
if(str[i]!=')') 
{
st.push(str[i])
}
//if the character is '(' the check for above 
//mentioned possibilites
else if(str[i]==')') 
{
//declare a boolean variable to check for 
//immediate popped element condition.
bool flag=true  
//variable to store temporary top elements.
int x=st.top()  
st.pop()
while(x!='(')
{
// Check for operators in expression 
if(str[i]=='+'||str[i]=='-'||str[i]=='/||str[i]=='^'||str[i]=='*')
flag=false
x=st.top()
st.pop()
}
//if there is immediate bracket without any 
//operator then its redundant bracket.
if(flag==true)
{return "YES"}
}
}
//there is no redundant brackets.
return "NO"
}

Time Complexity for above approach is: O(n)
Space Complexity for above approach is: O(n)

上述方法的时间复杂度为:O(n)
上述方法的空间复杂度为:O(n)

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll t;
cout << "Enter number of test cases: ";
cin >> t;
while (t--) {
string str;
cout << "Enter string: ";
cin >> str;
stack<char> st;
for (int i = 0; i < str.length(); i++) {
//is character is not ')' then push it into the stack.
if (str[i] != ')') 
st.push(str[i]);
//if the character is '(' the check for 
//above mentioned possibilites
else { 
//declare a boolean variable to check for 
//immediate popped element condition.
bool flag = true; 
char x = st.top(); //variable to store temporary top elements.
st.pop();
while (x != '(') {
// Check for operators in expression
if (x == '+' || x == '-' || x == '*' || x == '^' || x == '/')
flag = false;
x = st.top();
st.pop();
}
//if there is immediate bracket without any operator 
//then its redundant bracket.
if (flag == true) {
cout << "YES"
<< "\n";
goto end;
}
}
}
cout << "NO"
<< "\n";
end:;
}
return 0;
}

Output

输出量

Enter number of test cases: 4
Enter string: (a+b)
NO
Enter string: ((a+b))
YES
Enter string: (a+(b*c)-(d+e))
NO
Enter string: (a)
YES

2) Implementation

2)实施

Instead of using the stack to check redundancy, we make two variables to check the number of operators and the number of brackets and check for the condition if some character is present without any operators.

我们使用两个变量来检查运算符的数量和方括号的数量,并检查条件是否存在一些没有任何运算符的字符,而不是使用堆栈来检查冗余。

Pseudo Code:

伪代码:

string Redundant(string str)
{
//declare two variable for bracket and 
//operator respectively.
int x,y  
x=0  //initialise them with 0
y=0
//iterate through loop
for(int i=0;i<str.length();i++)
{
//if there is immediate breacket return yes
if(str[i]=='(' and str[i+2]==')')
return "YES"
//count number of '('
if(str[i]=='(')
x++;
//count number of operators.
if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/'||str[i]=='^')
y++;
}
//if number of breacket is higher than operator 
//then its redundant else its not redundant.
if(x>y)
return "YES"
else		
return "NO"
}

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll t;
cout << "Enter number of test cases: ";
cin >> t;
while (t--) {
string str;
cout << "Enter string: ";
cin >> str;
int x = 0;
int y = 0;
for (int i = 0; i < str.length(); i++) {
//immediate bracket without any operators
if (str[i] == '(' and str[i + 2] == ')') {
cout << "YES"
<< "\n";
goto end;
}
else if (str[i] == '(')
x++;
if (str[i] == '+' || str[i] == '-' || str[i] == '^' || str[i] == '*' || str[i] == '/')
y++;
}
//if number of brackets is greater than its redundant.
if (x > y)
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
end:;
}
return 0;
}

Output

输出量

Enter number of test cases: 3
Enter string: (a)
YES
Enter string: (a+(b-c))
NO
Enter string: (a+(b*c)-d+c)
NO

翻译自: https://www.includehelp.com/icp/redundant-bracket.aspx

热冗余冷冗余

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543738.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

对称树

Problem statement: 问题陈述&#xff1a; Given a binary Tree, check whether the tree is symmetric or not. 给定二叉树 &#xff0c; 检查树是否对称 。 Input Example: 输入示例&#xff1a; For example1/ \2 2/ \ / \3 4 4 3The above tree is symmetric1/ \2 …

unity, undo

如果在操作一个Object之前调用Undo.RecordObject(Object)&#xff0c;且操作确实造成Object某些属性的改变&#xff0c;则会产生一个undo记录。 如果我们的架构不是直接操作Object&#xff0c;而是操作一个ui&#xff0c;并在某些时机通过ui.save(Object)将数据回写到Object&am…

ajax应用_AJAX的应用

ajax应用AJAX has several benefits that can be utilized inside a web application. In this article, well explore some advantages of AJAX and see some of its applications. AJAX具有可在Web应用程序内部使用的多个优点。 在本文中&#xff0c;我们将探讨AJAX的一些优势…

Linux下DRBD配置

一、什么是DRBD1、简介 Distributed Replicated Block Device(DRBD)是一个用软件实现的、无共享的、服务器之间镜像块设备内容的存储复制解决方案。数据镜像&#xff1a;实时、透明、同步&#xff08;所有服务器都成功后返回&#xff09;、异步&#xff08;本地服务器成功后返回…

键盘特殊_特殊键盘

键盘特殊Problem statement: 问题陈述&#xff1a; Imagine you have a special keyboard with four types of keys: 想象一下&#xff0c;您有一个特殊的键盘&#xff0c;其中包含四种类型的键&#xff1a; Key 1: Prints I on screen 按键1&#xff1a;在屏幕上打印“ I”…

【C++入门】简单的日期类操作

//--------------------------------------------------------------------------/***名称&#xff1a;日期的简单操作******类函数&#xff1a;构造函数&#xff0c;拷贝构造函数&#xff0c;析构函数&#xff0c;操作符重载函数****日期类操作函数&#xff1a; 1&#xff1a;…

Scala山脉

Scala Range Scala山脉 A Range is a bounded series with a uniform interval with an upper and lower limit. The range literal is a numerical sequence of number ranging with a certain limit. 范围是一个有上限且下限均匀的有界序列。 范围文字是具有一定限制的范围…

黑客经验谈:跳板攻击入侵技术实例解析

网络入侵&#xff0c;安全第一,一个高明的入侵者&#xff0c;不会冒然实行动. 他们在入侵时前会做足功课&#xff0c;入侵时会通过各种技术手段保护自己&#xff0c;以防被对方发现&#xff0c;引火烧身. 其中&#xff0c;跳板技术是攻击者通常采用的技术. 下面笔者结合实例&am…

dom属性和html属性_HTML属性

dom属性和html属性Attributes are used to provide additional information of a tag such as it’s alignments, color, size of the text and other. The attributes are given with the tag that is between the angular brackets after the tag name. The attributes have …

科普:UTF-8 GBK UTF8 GB2312 之间的区别和关系

UTF-8&#xff1a;Unicode TransformationFormat-8bit&#xff0c;允许含BOM&#xff0c;但通常不含BOM。是用以解决国际上字符的一种多字节编码&#xff0c;它对英文使用8位&#xff08;即一个字节&#xff09;&#xff0c;中文使用24为&#xff08;三个字节&#xff09;来编码…

vue3实现本地开发使用的px转换成vw,px转换成rem方法整理

前言&#xff1a; 项目中如果想本地开发使用px&#xff0c;但是界面上线以后界面是自适应的效果,可以有多种方式来实现效果。 一、px转成vw 1、安装&#xff0c;安装成功后&#xff0c;node_modules 会新增这两个插件包 npm i postcss-px-to-viewport-8-plugin 2、新增 post…

airplay2协议是什么_什么是AirPlay?

airplay2协议是什么AirPlay (AirPlay) AirPlay is released by Apple in the year 2004. It allows the easy exchange of audios without the use of any wired technique between the two devices. It was previously termed as AirTunes and later got its name changed to …

微信支付开发(5) 订单查询

本文介绍微信支付中订单查询功能的实现。 作者&#xff1a;方倍工作室 地址&#xff1a;http://www.cnblogs.com/txw1958/p/wxpay-order-query.html 一、订单查询 因为某一方技术的原因&#xff0c;可能导致商户在预期时间内都收不到最终支付通知&#xff0c;此时商户可以通过该…

ruby 执行函数_Ruby at()函数

ruby 执行函数Ruby中的at()函数 (at() function in Ruby) If you are working with arrays in Ruby, sometimes you may need to find the element at a particular index. For meeting the purpose, we have got at() function in Ruby which is already defined in Rubys lib…

python饼形图_Python | 饼形图

python饼形图A pie plot or a pie chart is a circular statistical graphic technique, in which a circle is divided into slices with respect to numerical proportion. In a pie chart, the arc length, central angle, and area of each slice, is proportional to the …

Linux巡检

# uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostname # 查看计算机名 # lspci -tv # 列出所有PCI设备 # lsusb -tv # 列出所有USB设备 # lsmod # 列出加载的内核模块 # env # 查看环境变量 # fre…

appweb ejs_EJS部分

appweb ejsHi! Welcome. Today, we are going to look at EJS partials. EJS Partials help us avoid repetition of the same code on several web pages. 嗨&#xff01; 欢迎。 今天&#xff0c;我们将看EJS局​​部函数 。 EJS Partials帮助我们避免在多个网页上重复相同的…

Struts2配置

1. 设定server a) window– preferences – myeclipse – servers – tomcat – 6.x b) 选择tomcat homedirectory c) 选择enable d) finish 2. 设定jdk环境 a) window– preferences – java – installed jres b) 如果没有对应的JDK…

ruby继承_Ruby继承

ruby继承Ruby中的继承 (Inheritance in Ruby) Inheritance is a feature of Object Oriented languages in which new classes are derived from existing classes and resulting in the formation of a hierarchy of classes. The derived class is often called as child cla…

Spring与Hibernate整合中,使用OpenSessionInViewFilter后出现sessionFactory未注入问题

近期在知乎看到一句话&#xff0c;保持学习的有一种是你看到了很多其它的牛人&#xff0c;不甘心&#xff0c;真的不甘心。Spring和hibernate整合的时候&#xff0c;jsp页面做展现&#xff0c;发现展现属性出现&#xff1a; org.apache.jasper.JasperException: could not init…