热冗余冷冗余_冗余支架
熱冗余冷冗余
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: NOhere 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 rightfor(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 possibiliteselse 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=falsex=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 possibiliteselse { //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 expressionif (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) YES2) 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 0y=0//iterate through loopfor(int i=0;i<str.length();i++){//if there is immediate breacket return yesif(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 operatorsif (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";elsecout << "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
熱冗余冷冗余
總結
以上是生活随笔為你收集整理的热冗余冷冗余_冗余支架的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中intvalue_Java B
- 下一篇: 一个事物两个方面的对比举例_顶管施工也有