跳到主要內容

發表文章

目前顯示的是有「Algorithm」標籤的文章

[ACM] Some hints about uva 104 (Modified Floyd-Warshall)

by gits Original Thread http://online-judge.uva.es/board/viewtopic.php?t=7292 Well, I'll assume you understand what the problem asks. You have to find the shortest sequence that yelds a profit (not the one with the greatest profit!). If there is more then one sequence with the same length, any of those is valid. Now, you can't just try with brute force (trying all combinations) because it'll be too slow and you'll get Time Limit Exceeded. However, there's a well known algorithm, Floyd-Warshall, which will find all the shortest paths between every node to the others in just O(n^3) time. You can find more info about F-W in the net. In my previous post I said how you have to change the general F-W algorithm to work for this particular problem... As for floating point errors, most numbers representation isn't totally acurate; for instance, 0.1 is usually stored as 0.10000000000000001. After some operations, the error may influence the final result. Again, sea...

[Algo] String Matching - KMP Algorithm

看了兩次,可是每次都忘記,所以做個筆記 s: input string p: pattern Failure Function的值k,就是下次要從p(k)開始matching 舉例 Pattern A B C A B C D failure -1-1-1 0 1 2-1 Case I: Input string AB C D ADFGACEDAABBBB AB C A BCD 紅色 =not matching step1: 看最後一個matching字元 ( 綠色的字 ) 的failure function value= f step2: 將p(f)對齊綠色的位置 ( p(i)代表p的第i個字元 ) ex. f(C) = -1 ,所以p(-1)對齊上面那排的C AB C DADFGACEDAABB >>> ABCABCD Case II: ABCAB C E ACAADDBBBABCABC ABCAB C D f(C)=2 ,所以p(2)對齊上面那排的綠色C ABCAB C E ACAADDBBBABCABC >>> AB C ABCD 講的不是很有條理,不過我先給自己看懂就好= = 改天再來整理的清楚一些