在英文中,有一些标点符号需要成对使用,达到闭合的效果。例如双引号("") 大括号({}) 方括号([])
现在我们需要检测指定文本中的 双引号,大括号, 方括号是否闭合
由若干字母,空格,标点符号组合而成的长度为N, (0<= N <1000)的字符串
双引号,大括号, 方括号 都闭合,返回 true ; 否则返回false;
"I like apple!"
true
I want to go to the zoo [ the small one
false
"{}"
true
let input = readline(); let num1 = input.match(/\"/g)==null?0:input.match(/\"/g).length; let num2 = input.match(/\[/g)==null?0:input.match(/\[/g).length; let num3 = input.match(/\]/g)==null?0:input.match(/\]/g).length; let num4 = input.match(/\{/g)==null?0:input.match(/\{/g).length; let num5 = input.match(/\}/g)==null?0:input.match(/\}/g).length; console.log((num2==num3)&&(num4==num5)&&!(num1%2));
s=input().strip() t=[] for i in range(len(s)): f=0 if s[i]!=')' or s[i]!='}' or s[i]!=']': t.append(s[i]) elif s[i]==')': while t[-1]!='(' and len(t)>0: if t[-1]=='{' or t[-1]=='[': f=1 break t.pop() t.pop() elif s[i]=='}': while t[-1]!='{' and len(t)>0: if t[-1]=='(' or t[-1]=='[': f=1 break t.pop() t.pop() else: while t[-1]!='[' and len(t)>0: if t[-1]=='(' or t[-1]=='{': f=1 break t.pop() t.pop() if f==1:break if '"' in t: if t.count('"')%2 !=0:f=1 if f==1 or ('[' in t) or ('{' in t) or ('(' in t):print('false') else:print('true')
#include<iostream> #include<string> #include<stack> using namespace std; int main() { string str; getline(cin, str); int i = 0, num = 0; stack<char> st; while(i < str.size()) { if(str[i] == '[' || str[i] == '{') st.push(str[i]); else if(str[i] == '"') { ++num; } else if(str[i] == ']') { if(st.top() != '[') break; else st.pop(); } else if(str[i] == '}') { if(st.top() != '{') break; else st.pop(); } ++i; } bool res = st.empty() && !(num % 2); cout << boolalpha << res << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ string s; stack<char> stk; getline(cin,s); for(char c:s){ if(c=='['||c=='{') stk.push(c); else if(c=='"'){ if(stk.empty()||stk.top()!='"') stk.push(c); else stk.pop(); }else if(c==']'){ if(stk.top()!='[') break; else stk.pop(); }else if(c=='}'){ if(stk.top()!='{') break; else stk.pop(); } } if(!stk.empty()) cout<<"false"<<endl; else cout<<"true"<<endl; return 0; }
//双引号,大括号, 方括号 都闭合 let str = readline(); let leftcount = 0; let rightcount = 0; let count = 0; //console.log(str); //console.log(str[str.length-1]); for(let i = 0; i < str.length; i++) { if(str[i]==='"') { count++; } if(str[i]==='[') { leftcount++; } if(str[i]===']') { rightcount++; } if(str[i]==='{') { leftcount++; } if(str[i]==='}') { rightcount++; } } //console.log(count); let flag = false; if(rightcount===leftcount && count%2===0) { flag = true; } if(flag){ console.log('true'); } else { console.log('false'); }随便做做,能A就行
import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { Scanner sr = new Scanner(System.in); String s = sr.nextLine(); Stack<Character> stack = new Stack<Character>(); boolean flag=true; for(int i=0;i<s.length();i++){ char ch = s.charAt(i); if(ch=='[' || ch=='{'){ stack.add(ch); } else if(ch=='"'){//如果栈顶也是"则匹配 if(!stack.empty()&&stack.peek()==ch) stack.pop(); else stack.add(ch); } //检查[] else if(ch==']'){ if(stack.empty()){ flag=false; break; } char ch1 =stack.pop(); if(ch1!='['){ flag=false; break; } } //检查{} else if(ch=='}'){ if(stack.empty()){ flag=false; break; } char ch2 = stack.pop(); if(ch2!='{'){ flag=false; break; } } } if(flag&&!stack.isEmpty()) flag=false; System.out.print(flag); } }
#include <bits/stdc++.h> #include <unordered_map> using namespace std; int main() { char ch; unordered_map<char, char> mp; stack<char> s; mp['}'] = '{'; mp[']'] = '['; mp['"'] = '"'; while ((ch = getchar()) != '\n') { if (ch == '{' || ch == '[') { s.push(ch); } else if (ch == '}' || ch == ']') { if (s.empty()) { cout << "false"; return 0; } else { char t = s.top(); if (mp[ch] == t) { s.pop(); } else { cout << "false"; return 0; } } } else if (ch == '"') { if (s.empty() || s.top() != '"') { s.push(ch); } else { s.pop(); } } } if (s.empty()) { cout << "true"; } else { cout << "false"; } return 0; }