b275. 87 全國資訊學科能力競賽(1998)|括號匹配

中等字串學科能力競賽堆疊括號匹配索引映射整行輸入教學回報錯誤

時間限制 20000 ms ・ 記憶體限制 512 MB

題目描述

每個左括號由內而外與右側最近的可匹配右括號配對。判斷每行文字是否有未匹配括號;若有,在原字串下方對應字元位置用!標示。非括號字元不參與配對,但保留其位置。

原題:http://cs.cysh.cy.edu.tw/competition_problem_set/全國87.doc 未取得官方測資;測資均為自製且以獨立方法驗證。原題以檔案讀取,本練習採標準輸入輸出。原題未載單題執行時限或記憶體上限;本站練習設定20秒、512MB。 原競賽評分:判斷是否含未匹配括號50%,標示位置50%;本站練習採全部測資通過制,不模擬原分項給分。

輸入格式

每行一個待檢查字串,讀至檔尾。原題未另指定字串長度上限。

輸出格式

每筆先印The test string is:,下一行原字串。若不正確,再印由空白與!構成的標示行(!位於未匹配括號下方),然後Result: This string is wrong.;正確則直接印Result: This string is correct.。每筆最後空一行。

測資說明

Generated tests; independent oracle validated. No official testdata located.

範例測資

範例輸入 1

(a+b+c)
(a*(b-c)
(a+b))+(c*d)
***--aaa))
^^^)(&&&
(abc^^^)())))

範例輸出 1

The test string is:
(a+b+c)
Result: This string is correct.

The test string is:
(a*(b-c)
!
Result: This string is wrong.

The test string is:
(a+b))+(c*d)
     !
Result: This string is wrong.

The test string is:
***--aaa))
        !!
Result: This string is wrong.

The test string is:
^^^)(&&&
   !!
Result: This string is wrong.

The test string is:
(abc^^^)())))
          !!!
Result: This string is wrong.

請先登入後再提交程式碼

討論與題解

載入討論區…

更多