1. 程式人生 > >資料結構實驗之棧四:括號匹配(棧的運用)

資料結構實驗之棧四:括號匹配(棧的運用)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int i,j,n,l;
    char s[150],p[150];
    while(gets(s)!=NULL)
    {
        n=strlen(s);
        j=0;                                      //標示符號
        l=1;
        for(i=0; i<n; i++)
        {
            if(s[i]=='{'||s[i]=='('||s[i]=='[')
            {
                p[j++]=s[i];
            }
            if(s[i]==')')
            {
                if(j==0)
                {
                    l=0;
                    break;
                }
                if(j>0&&p[j-1]=='(')              //與上一個元素比較
                {
                    j=j-1;
                }
                else if(j>0&&p[j-1]!='(')
                {
                    l=0;
                    break;
                }
            }
            if(s[i]==']')
            {
                if(j==0)
                {
                    l=0;
                    break;
                }
                if(j>0&&p[j-1]=='[')
                {
                    j=j-1;
                }
                else if(j>0&&p[j-1]!='[')
                {
                    l=0;
                    break;
                }
            }
            if(s[i]=='}')
            {
                if(j==0)
                {
                    l=0;
                    break;
                }
                if(j>0&&p[j-1]=='{')
                {
                    j=j-1;
                }
                else if(j>0&&p[j-1]!='{')
                {
                    l=0;
                    break;
                }
            }
        }
        if(l==0||j>0)
            printf("no\n");
        else if(j==0)
            printf("yes\n");
    }
    return 0;
}


相關推薦

資料結構實驗括號匹配運用

#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int i,j,n,l; char s[150],p[150]; while(gets(s)!=NULL)

資料結構實驗查詢六順序查詢SDUT 3378

 (不知道為啥開個陣列就 TLE 。QAQ) #include <stdio.h> #include <stdlib.h> #include <string.h> //const int maxn = 100005; // //int a[ma

資料結構實驗排序七選課名單 SDUT 3404

#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { char data[15]; struct node *next; //存放名字 }; st

資料結構實驗與佇列括號匹配SDUT 2134

#include <bits/stdc++.h> using namespace std; typedef long long ll; char s[100]; char a[100]; int main() { int i,j,k,f,top,len; while(

資料結構實驗查詢二分查詢__Find

Time Limit: 30 ms Memory Limit: 65536 KiB Problem Description 在一個給定的無重複元素的遞增序列裡,查詢與給定關鍵字相同的元素,若存在則輸出找到的位置,不存在輸出-1。 Input 一組輸入資料,輸入資料第一行首先輸入兩

資料結構實驗排序尋找大富翁SDUT 3401

#include <stdio.h> #include <stdlib.h> #include <string.h> void Swap(int a[], int i, int j) // 交換函式 { int t = a[i]; a[i]

SDUT3376資料結構實驗查詢二分查詢

簡單題,就不多說了,不會的對答案,或者評論也行,哈哈 #include <iostream> #include <cstring> #include <cstdio> using namespace std; int a[1000010]; int

SDUT-3376_資料結構實驗查詢二分查詢

資料結構實驗之查詢四:二分查詢 Time Limit: 30 ms Memory Limit: 65536 KiB Problem Description 在一個給定的無重複元素的遞增序列裡,查詢與給定關鍵字相同的元素,若存在則輸出找到的位置,不存在輸出-1。 Input 一組輸入資料,輸入資料第一

SDUT3401資料結構實驗排序尋找大富翁

c中的快排 此題出題者本意應該是讓我們用堆排序去做,但是鄙人只知何為堆,不知如何做,故投機取巧,用了c語言中的快排qsort(); 這個快排在stdlib標頭檔案下 格式為 qsort(陣列的起始地址即陣列名,元素的個數,每個元素的大小(位元組數),自定義函式(指向函式的方向指標,用於排

資料結構實驗排序尋找大富翁堆排序

資料結構實驗之排序四:尋找大富翁 Time Limit: 200 ms Memory Limit: 512 KiB Problem Description 2015胡潤全球財富榜調查顯示,個人資產在1000萬以上的高淨值人 群達到200萬人,假設給出N個人的個人資產值,請你快速找出排前M位的

堆排——資料結構實驗排序尋找大富翁

資料結構實驗之排序四:尋找大富翁 Time Limit: 200 ms Memory Limit: 512 KiB Submit Statistic Problem Description 2015胡潤全球財富榜調查顯示,個人資產在1000萬以上的高淨值人群達到2

資料結構實驗排序七選課名單裡面包含了新思想!!!!好神奇

Attention: 如果struct node 裡的陣列開的太大,會導致MLE!!!   資料結構實驗之排序七:選課名單 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic

SDUT OJ 資料結構實驗串二字串匹配(KMP做法)

資料結構實驗之串二:字串匹配 Time Limit: 1000MS Memory limit: 65536K 題目描述   給定兩個字串string1和string2,判斷strin

資料結構實驗括號匹配()

Think: 模擬棧, 每次判斷 當前字元若為 ( [ { 如果是的話就壓入棧中 當前字元若為)] } 的話就判斷棧頂元素是否為相對應的括弧 Problem Description 給你一串字元,不超過50個字元,可能包括括號、數字、字母、標

2134資料結構實驗括號匹配

Time Limit: 1000MS Memory limit: 65536K 題目描述  給你一串字元,不超過50個字元,可能包括括號、數字、字母、標點符號、空格,你的任務是檢查這一串字元

資料結構實驗括號匹配

Problem Description 給你一串字元,不超過50個字元,可能包括括號、數字、字母、標點符號、空格,你的任務是檢查這一串字元中的( ) ,[ ],{ }是否匹配。 Input 輸

SDUT OJ 資料結構實驗括號匹配

#include<iostream> #include<stdio.h> using namespace std; int main() { char a[51],b[51]; int i,top; while(gets(a)!=NULL)

資料結構實驗括號匹配 字串匹配

yes no 每當遇到( { 【 的時候 資料進棧 遇到)}】的時候出棧頂 看是否能匹配 如果能 則出棧 否則 結束 注意輸入的時候有空格 #include<cstdio> #include<stack> #include<cstring> #inclu

資料結構實驗陣列三快速轉置std::stable_sort函式

資料結構實驗之陣列三:快速轉置 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Description   轉置

SDUTOJ3311資料結構實驗串三KMP應用

資料結構實驗之串三:KMP應用 (PS:這題巨坑  嗚嗚嗚。。) https://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2710/pid/3311 Time Limit:&nbs