8 solutions
-
1
#include <bits/stdc++.h> using namespace std; stack<char> s; string c; int main(int argc, char **argv){ getline(cin,c); for (int i = 0;c[i];i++){ if (c[i] == '(') s.push('('); if (c[i] == '[') s.push('['); if (c[i] == ')'){ if (!s.empty()){ // 先检测是否为空,不然会 RE if (s.top() != '('){ // 如果不匹配,直接退出 printf("Wrong"); return 0; } } s.pop(); // 否则把 '(' 出栈 } if (c[i] == ']'){ // 同理 if (!s.empty()){ if (s.top() != '['){ printf("Wrong"); return 0; } } s.pop(); } } if (s.empty()) printf("OK"); // 如果没剩下括号就 OK else printf("Wrong"); // 否则错误 return 0; }
-
0
用栈怎么做?
#include <bits/stdc++.h> using namespace std; char n[256]; bool a; int x = 1; int sum1 = 0; int num1 = 0; int sum2 = 0; int num2 = 0; int main() { while(cin >> n[x]) { if(n[x] == '(') { sum1++; } else if(n[x] == ')') { num1++; if(n[x-1]!='('&&n[x-1]!=']'&&n[x-1]!=')') { cout << "Wrong"; return 0; } if(num1>sum1) { cout << "Wrong"; return 0; } } else if(n[x] == '[') { sum2++; } else if(n[x] == ']') { num2++; if(n[x-1] !='['&&n[x-1] != ')'&&n[x-1]!=']') { cout << "Wrong"; return 0; } if(sum2<0) { cout << "Wrong"; return 0; } } x++; } if(sum1!=num1||sum2!=num2) { cout << "Wrong"; return 0; } cout << "OK"; return 0; }
-
0
#include <bits/stdc++.h> using namespace std; /* 自测样例:*/ // ((())))) // ((()))) // )))) stack <char> s; char a; char mp[260]; bool flag = false; string sss; int main() { mp[')'] = '('; mp[']'] = '['; while (cin >> a) { flag = true; if (a == '(' || a == '[') { s.push(a); } else if (a == ')' || a == ']') { if (!s.empty() && s.top() == mp[a]) s.pop(); else { cout << "Wrong"; return 0; } } } if (s.empty() && flag) cout << "OK"; else cout << "Wrong"; return 0; }
-
0
#include #include #include #include using namespace std;
stack s;
int main() { string str; getline(cin, str); int len = str.length(), cnt = 1; char c; for (int i = 0; i < len; i++) { if (str[i] == '(' || str[i] == '[') { s.push(str[i]); } else if (str[i] == ')') { if (s.empty()) { cnt = 0; break;
} c = s.top(); if (c == '(') { s.pop(); } else { cnt = 0; break;
} } else if (str[i] == ']') { if (s.empty()) { cnt = 0; break; } c = s.top(); if (c == '[') { s.pop(); } else { cnt = 0; break; } }
} if (s.empty() && cnt != 0) { cout << "OK" << endl; } else { cout << "Wrong" << endl; } return 0;
}
-
0
#include<iostream> #include<stack> #include<string> using namespace std; int main() { stack<char>k; char a; string s; getline(cin,s);//输入 for(int i=0;i<s.size();i++)//遍历 { a=s[i]; if(a=='(')//当一个新的域时 { k.push('(');//添加 } else if(a=='[')//当一个新的域时 { k.push('[');//添加 } else if(a==')')//当结束一个域时 { if(k.empty())//为空时 { k.push('0'); break; } else if(k.top()=='(') { k.pop();//弹出 } } else if(a==']')//当结束一个域时 { if(k.empty())//为空时 { k.push('0'); break; } if(k.top()=='[') { k.pop();//弹出 } } } if(k.empty())//输出 { cout<<"OK"; } else { cout<<"Wrong"; } return 0;//完结散花 }
-
0
注意!!!!样例是中文的括号!!!
#include<bits/stdc++.h> using namespace std; int main(){ stack<char> a; char n; while(cin>>n){ if(n==')' || n=='('){ if(n==')' && a.size()==0){ cout<<"Wrong";//判断是否对的上 return 0; } if(n=='(') a.push(n);//入栈 if(n==')'){ if(a.top()=='(') a.pop(); else{ cout<<"Wrong"; return 0; } } } else if(n=='[' || n==']'){//中括号 if(n==']' && a.size()==0){ cout<<"Wrong"; return 0; } if(n=='[') a.push(n); if(n==']'){ if(a.top()=='[') a.pop(); else{ cout<<"Wrong"; return 0; } } } } //如果栈里没完全被消掉,则代表多出 //若消掉,则代表没有多余的括号(匹配上了) if(a.size()!=0) cout<<"Wrong"; else cout<<"OK"; return 0; }
-
-1
#include <bits/stdc++.h> using namespace std; string a; stack<char>s1; int main(){ cin>>a; for(int i=0;i<=a.size();i++){ if(a[i]=='('||a[i]=='['){ s1.push(a[i]); } else if(a[i]==')'||a[i]==']'){ if(s1.empty()==true){ cout<<"Wrong"; return 0; } else { if((s1.top()=='('&&a[i]==']')||(s1.top()=='['&&a[i]==')')){ cout<<"Wrong"; return 0; } else{ s1.pop(); } } } } if(s1.empty()==true){ cout<<"OK"; return 0; } else{ cout<<"Wrong"; return 0; } }
- 1
Information
- ID
- 839
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 7
- Tags
- # Submissions
- 165
- Accepted
- 36
- Uploaded By