2010-11-13 45 views

回答

1

input: lineopen (line having opening tag) 
input: text  (test as array of lines) 
output: lineclose (line having closing tag) 

line := lineopen 
counter := 1 
do 
    line := line + 1 
    if text[line] contains opening tag 
     then counter := counter + 1 
    if text[line] contains closing tag 
     then counter := counter - 1 
while counter > 0 
lineclose := line 
3

傳統的方式做,這是一個堆棧。當你得到一個開放標籤時推,當你得到一個關閉標籤時拉。從空堆棧拖出或在末尾有非零堆棧表示不匹配的標籤。直到筆者要檢查標籤校驗

+0

簡單列入計數器(增加它在打開的標籤,在收盤減少)就足夠了。 – Vovanium 2010-11-13 17:37:27