看板 Prob_Solve 關於我們 聯絡資訊
※ 引述《RockLee (Now of all times)》之銘言: : 原始網址: : http://www.careercup.com/question?id=14539805 : 題目: : Three strings say A, B, C are given to you. : Check weather 3rd string is interleaved from string A and B. : Ex: A="abcd" B="xyz" C="axybczd". answer is yes. o(n) : 用 Dynamic Programming 應該可在 O(n^2) 的時間內解決 : 但要在 O(n) 的時間內解決就想不出來了 Orz... : CareerCup 上的討論看來都無法在 O(n) 的時間內正確的解決 : 不知道板上有沒有人有什麼 idea? 這是一個很快的想法, 我不知道對不對, 反正大家討論一下. You can put two flags on string A and B, then scan every character in C. for example, the first step, it will be, A = a | bcd and B = | xyz. then it's not hard to see if the flag of A,B move to the end. One problem is the one it points out in the webpage. if A = 'ca', B = 'cb' , C = 'cacb' or 'cbca' should be both true. So, when you scan the character, you need to put an additional index to handle the duplicate character. In this situation, C = 'cacb', when you do the scan of the fist char A = c | a and B = c | b, but both has index = 1. In the next step, you scan second char, which is a, Now, A = 'c a |' and you reset the index of A into 0. at the same time you push the flag of B back with 1. So B = '| c b'. This should be running in a linear time? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 142.136.127.217
isnoneval:A=xy, B=xxxy, C=xxyxxy? 02/11 14:02
Leon:恩, 妳是對的, 這個情況會越長越多, 不能只記一個 02/11 16:35
Leon:不過, 我猜 duplicate char 可以另外的方式處理, 我想想 02/11 16:40