首页 > 试题广场 >

闭合标点符号检测

[编程题]闭合标点符号检测
  • 热度指数:564 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
在英文中,有一些标点符号需要成对使用,达到闭合的效果。例如双引号("") 大括号({}) 方括号([])
现在我们需要检测指定文本中的 双引号,大括号, 方括号是否闭合

输入描述:
由若干字母,空格,标点符号组合而成的长度为N, (0<= N <1000)的字符串


输出描述:
双引号,大括号, 方括号 都闭合,返回 true ; 否则返回false;
示例1

输入

"I like apple!"

输出

true
示例2

输入

I want to go to the zoo [ the small one

输出

false
示例3

输入

"{}"

输出

true
发表于 2022-04-25 16:32:00 回复(0)
str=input()
if str.count("\"")%2!=0:    #如果存在引号但为奇数,则错误
    print("false")
elif str.count("[")!=str.count("]"):    #如果[ 数目!= ]数目,则错误
    print("false")
elif str.count("{")!=str.count("}"):    #如果{ 数目!= }数目,则错误
    print("false")
else:
    print("true")
发表于 2022-09-01 10:21:09 回复(0)
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)); 
发表于 2021-09-12 23:48:35 回复(0)
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')

编辑于 2021-06-04 15:34:53 回复(0)
#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;
}

发表于 2021-03-27 11:01:40 回复(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;
}

发表于 2021-03-19 21:29:43 回复(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就行
发表于 2021-03-19 20:52:31 回复(2)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String s = reader.readLine();
        Stack<Character> stack = new Stack<>();
        int num = 0;
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == '{')
            {
                stack.push('}');
            }else if(s.charAt(i) == '"')
            {
                num++;
            }else if(s.charAt(i) == '[')
            {
                stack.push(']');
            }else if(s.charAt(i) == ']' || s.charAt(i) == '}'){
                if(stack.peek()!=s.charAt(i))
                {
                    System.out.println("false");
                    return;
                }else{
                    stack.pop();
                }
            }
        }
        if(!stack.isEmpty()||num%2!=0)
        {
            System.out.println("false");
            return;
        }
        System.out.println("true");

    }
}
发表于 2021-03-18 21:04:53 回复(0)
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);
    }
}

发表于 2021-03-12 19:53:27 回复(0)
5sy头像 5sy
str=input()
if str.count("\"")%2!=0:    #如果存在引号但为奇数,则错误
    print("false")
elif str.count("[")!=str.count("]"):    #如果[ 数目!= ]数目,则错误
    print("false")
elif str.count("{")!=str.count("}"):    #如果{ 数目!= }数目,则错误
    print("false")
else:
    print("true")

发表于 2021-03-10 14:25:12 回复(1)
import re
s=re.sub(r'[^\{\}\[\]\(\)\"\"]','',input())
 
while 1:
    s1=re.sub(r'\{\}|\[\]|\"\"|\(\)','',s)
    if s1==s:
        break
    s=s1
print(['false','true'][not s])
消 消 乐
发表于 2021-03-09 21:04:50 回复(0)
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();
    char[] arr = s.toCharArray();
    System.out.println(solution(s, arr));;
}
      static boolean solution(String s,char arr[]){
        //声明一个栈,我的版本太低 不支持泛型 菱形运算符
       Stack<Character> stack = new Stack<Character>();
        int flag=0;//开关 判断'"'符号匹配
        for(int i=0;i<s.length();i++){
            char ch = s.charAt(i);
            if(ch=='[' || ch=='{'){
                stack.add(ch);    
            }
            if(ch=='"'){
                stack.add(ch);
                flag+=1;
            }
            //检查[]
            else if(ch==']'){
                char ch1 =stack.pop();
                if(ch1!='[') return false;
            }
            //检查{}
            else if(ch=='}'){
                char ch2 = stack.pop();
                if(ch2!='{') return false;
            }
            //检查""
            

        }
        if(!stack.isEmpty()&&flag%2!=0) return false;
        return true;
   }
}

发表于 2021-03-05 21:14:05 回复(0)
#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;
}

发表于 2021-03-05 10:21:23 回复(0)