NYOJ 2 括弧配對問題

來源:互聯網
上載者:User

標籤:

括弧配對問題時間限制:3000 ms  |  記憶體限制:65535 KB 難度:3
描述
現在,有一行括弧序列,請你檢查這行括弧是否配對。
輸入
第一行輸入一個數N(0<N<=100),表示有N組測試資料。後面的N行輸入多組輸入資料,每組輸入資料都是一個字串S(S的長度小於10000,且S不是空串),測試資料群組數少於5組。資料保證S中只含有"[","]","(",")"四種字元
輸出
每組輸入資料的輸出佔一行,如果該字串中所含的括弧是配對的,則輸出Yes,如果不配對則輸出No
範例輸入
3[(])(])([[]()])
範例輸出
NoNoYes
 1 #include<stdio.h> 2 #include<string.h> 3 char s[10000+10],p[10000]; 4 int judgement() 5 { 6     int t,i,a; 7     scanf("%s",s); 8     t=strlen(s); 9     p[0]=‘o‘;10     for(i=0,a=1;i<t;i++)11     {12         p[a]=s[i];13         switch(s[i])14         {15             case‘(‘: a++;16             break;17 18             case‘[‘: a++;19             break;20 21             case‘)‘: if(p[a-1]==‘(‘) a--;22             else return 0;23             break;24 25             case‘]‘: if(p[a-1]==‘[‘) a--;26             else return 0;27             break;28         }29     }30     if(a==1)31         return 1;32     else33         return 0;34 }35 int main()36 {37     int n;38     scanf("%d",&n);39     while(n--)40     {41         if(!judgement())42             printf("No\n");43         else44         printf("Yes\n");45     }46     return 0;47 }

 

NYOJ 2 括弧配對問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.