热冗余冷冗余

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

热冗余冷冗余

热冗余冷冗余_冗余支架相关推荐

  1. mysql 冗余字段 同步_冗余数据一致性,到底如何保证?

    一,为什么要冗余数据 互联网数据量很大的业务场景,往往数据库需要进行水平切分来降低单库数据量. 水平切分会有一个patition key,通过patition key的查询能够直接定位到库,但是非pa ...

  2. rda冗余分析步骤_分子生态网络分析(MENA)构建微生物网络示例

    分子生态网络分析(MENA)构建微生物网络示例续前文"微生物共发生网络",本篇继续简介分子生态网络分析(Molecular Ecological Network Analysis, ...

  3. c语言冗余数据什么意思,冗余是什么意思_冗余解释和意思

    描述 大家知道冗余是什么意思吗?对于这些不常出现的词汇,你是否知道它们的意思?下面就和小编一块来了解一下冗余是什么意思吧. 冗余,拼音:rǒng yú,英文:[redundancy redundanc ...

  4. 从 S7-300/400 软冗余到 S7-1500R/H 冗余系统

    博途工控人平时在哪里技术交流博途工控人社群 博途工控人平时在哪里技术交流博途工控人社群 一.前      言 1994年,西门子发布了 S7-300 PLC 系统.模块化的结构,可自由灵活扩展的配置, ...

  5. dcs常用的冗余方式_DCS的冗余

    冗余技术就是增加多余的设备,以保证系统更加可靠.安全地工作.冗余的分类方法多种多样,按照在系统中所处的位置,冗余可分为元件级.部件级和系统级:按照冗余的程度可分为1:1冗余.1:2冗余.1:n冗余等多 ...

  6. 什么是同城冗余ZRS和本地冗余LRS?阿里云对象存储OSS详解

    阿里云对象存储OSS具有高可靠性,存储空间分为同城冗余ZRS和本地冗余LRS两种,阿里云百科来详细说下阿里云对象存储OSS冗余类型及同城冗余ZRS和本地冗余LRS的价格: 阿里云对象存储OSS 对象存 ...

  7. 【Elasticsearch】使用索引生命周期管理实现热温冷架构

    1.概述 [Elasticsearch]Elasticsearch 索引生命周期管理 转载:使用索引生命周期管理实现热温冷架构 索引生命周期管理 (ILM) 是在 Elasticsearch 6.6( ...

  8. 热锅热油,热锅冷油,热锅宽油,锅上火滑油,留底油区别

    热锅热油: 热锅热油就是把锅烧热,倒入油以后,等油也烧热(油烟冒出均匀)再把食材下锅.这种适合爆炒系列: 热锅冷油:(适用于煎炸但是容易糊的食物,如花生等) 热锅冷油就是先把锅烧热,然后倒入油,就直接 ...

  9. 彩票软件中热温冷算法的实现情况

    彩票软件中热温冷算法的实现情况 Delphi / Windows SDK/API http://www.delphi2007.net/DelphiDB/html/delphi_200612251912 ...

最新文章

  1. Spring Boot 对CORS跨域访问的配置
  2. c++ stl队列初始化_声明,初始化和访问向量| C ++ STL
  3. 最大正方形(洛谷-P1387)
  4. 五个很厉害的 CNN 架构
  5. ajax+对号,操作成功动画效果(圆圈变成勾号)
  6. 蓝桥杯 ALGO-129 算法训练 特殊的数字四十
  7. spring定时任务配置,以及不执行的解决办法
  8. 设计模式:JavaScript
  9. php 时分秒转时分_php 时分秒转为秒,秒转化为天时分秒
  10. Linux内核启动流程详解
  11. vue使用百度编辑器ueditor,ueditor1.5.0下载
  12. 解决Error:L6218E:Undefined symbol TimingDelay_Decrement (referred from stm32f2xx_it.o)问题
  13. iphone7plus启动时icon被拉伸放大的原因
  14. 体验godaddy域名转入,添加A记录,及使用dnspod的NS
  15. C语言每日一练——第159天:佩奇存钱方案
  16. 经典蓝牙与低功耗蓝牙的区别
  17. android alarmmanager 收不到广播,android定时闹钟:Service+BroadcastReceiver+AlarmManager+NotificationManager...
  18. 跨境新时代|2021 跨境电商趋势发展论坛成功举行
  19. java 连续运算_java 另类方法实现计算机连续四则运算
  20. IDOC报错Ship-to party XXX can not be used, message no.VP204

热门文章

  1. 计算机二级指针,C语言——二级指针
  2. python实现表格线性回归_Python实现线性回归
  3. CSS属性(display)
  4. Promise进阶——如何实现一个Promise库
  5. 高级软件工程第一次作业--准备
  6. MYSQL安装和配置
  7. 01json转字符串
  8. Python调用微博API获取微博内容
  9. SpringMVC 中设置日期字符串转换格式
  10. Gmap.net 怎么导入离线地图