锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲色大成网站www,亚洲精品综合在线影院,一本色道久久综合亚洲精品http://www.tkk7.com/xan/category/32224.html瀛︿範(fàn) 鎬濊?瀹炶返zh-cnFri, 03 Oct 2008 06:43:40 GMTFri, 03 Oct 2008 06:43:40 GMT60N! 鐨勪綅鏁?/title><link>http://www.tkk7.com/xan/archive/2008/10/03/232199.html</link><dc:creator>xan</dc:creator><author>xan</author><pubDate>Fri, 03 Oct 2008 06:20:00 GMT</pubDate><guid>http://www.tkk7.com/xan/archive/2008/10/03/232199.html</guid><wfw:comment>http://www.tkk7.com/xan/comments/232199.html</wfw:comment><comments>http://www.tkk7.com/xan/archive/2008/10/03/232199.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/xan/comments/commentRss/232199.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/xan/services/trackbacks/232199.html</trackback:ping><description><![CDATA[鏁癗鐨勪綅鏁?[lgN] + 1<br /> N! = N*(N-1)*...*1<br /> 浣嶆暟 [lgN!]+1=lg(N(N-1)...1)+1 = (lgN+lg(N-1)+...+lg1) + 1 <img src ="http://www.tkk7.com/xan/aggbug/232199.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/xan/" target="_blank">xan</a> 2008-10-03 14:20 <a href="http://www.tkk7.com/xan/archive/2008/10/03/232199.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>蹇熸帓搴?/title><link>http://www.tkk7.com/xan/archive/2008/10/03/232195.html</link><dc:creator>xan</dc:creator><author>xan</author><pubDate>Fri, 03 Oct 2008 06:10:00 GMT</pubDate><guid>http://www.tkk7.com/xan/archive/2008/10/03/232195.html</guid><wfw:comment>http://www.tkk7.com/xan/comments/232195.html</wfw:comment><comments>http://www.tkk7.com/xan/archive/2008/10/03/232195.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/xan/comments/commentRss/232195.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/xan/services/trackbacks/232195.html</trackback:ping><description><![CDATA[<p>瀹炶返涓渶蹇殑宸茬煡鎺掑簭綆楁硶, O(NlogN),鏈鍧廜(N<sup>2</sup>)<br /> loop:<br /> 1. 濡傛灉S涓厓绱犱釜鏁頒負(fù)0鎴栬?,榪斿洖<br /> 2. 鍙朣涓換鎰忓厓绱爒涓烘灑綰?br /> 3. 灝哠涓綑涓嬪厓绱犳寜>v 鍜?<v鍒嗘垚涓や釜涓嶅悓閮ㄥ垎<br /> 4. 瀵硅繖涓や釜閮ㄥ垎蹇熸帓搴?br /> <br /> 鏋㈢航鍏冮夋嫨:<br /> 涓鑸噰鐢⊿涓搗濮?緇撴潫,涓棿浣嶇疆鐨勪笁涓肩殑涓間負(fù)鏋㈢航鍏?(涓夋暟涓煎垎鍓叉硶)</p> <img src ="http://www.tkk7.com/xan/aggbug/232195.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/xan/" target="_blank">xan</a> 2008-10-03 14:10 <a href="http://www.tkk7.com/xan/archive/2008/10/03/232195.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>[zz]涓浗鍓╀綑鍏悊+鎵╁睍嬈у嚑閲屽痙綆楁硶http://www.tkk7.com/xan/archive/2008/09/26/231280.htmlxanxanFri, 26 Sep 2008 05:19:00 GMThttp://www.tkk7.com/xan/archive/2008/09/26/231280.htmlhttp://www.tkk7.com/xan/comments/231280.htmlhttp://www.tkk7.com/xan/archive/2008/09/26/231280.html#Feedback0http://www.tkk7.com/xan/comments/commentRss/231280.htmlhttp://www.tkk7.com/xan/services/trackbacks/231280.htmlhttp://en.wikipedia.org/wiki/Chinese_remainder_theorem & http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm ]

Suppose n1, n2, …, nk are positive integers which are pairwise coprime. Then, for any given integers a1,a2, …, ak, there exists an integer x solving the system of simultaneous congruences

\begin{align}
 x &\equiv a_1 \pmod{n_1} \\
 x &\equiv a_2 \pmod{n_2} \\
   &\vdots \\
 x &\equiv a_k \pmod{n_k}
\end{align}

Furthermore, all solutions x to this system are congruent modulo the product N = n1n2nk.

Hence x \equiv y \pmod{n_i} for all 1\leq i \leq k, if and only if x \equiv y \pmod{N}.

Sometimes, the simultaneous congruences can be solved even if the ni's are not pairwise coprime. A solution x exists if and only if:

a_i \equiv a_j \pmod{\gcd(n_i,n_j)} \qquad \mbox{for all }i\mbox{ and }j . \,\!

All solutions x are then congruent modulo the least common multiple of the ni.

Versions of the Chinese remainder theorem were also known to Brahmagupta (7th century), and appear in Liber Abaci (1202).

[edit] A constructive algorithm to find the solution

This algorithm only treats the situations where the ni's are coprime. The method of successive substitution can often yield solutions to simultaneous congruences, even when the moduli are not pairwise coprime.

Suppose, as above, that a solution is needed to the system of congruences:

x \equiv a_i \pmod{n_i} \quad\mathrm{for}\; i = 1, \ldots, k.

Again, to begin, the product  N=n_1n_2\ldots n_k is defined. Then a solution x can be found as follows.

For each i the integers ni and N / ni are coprime. Using the extended Euclidean algorithm we can therefore find integers ri and si such that rini + siN / ni = 1. Then, choosing the label ei = siN / ni, the above expression becomes:

 r_i n_i + e_i = 1 \,\!

Consider ei. The above equation guarantees that its remainder, when divided by ni, must be 1. On the other hand, since it is formed as siN / ni, the presence of N guarantees that it's evenly divisible by any nj so long as j\ne i.

e_i \equiv 1 \pmod{n_i} \quad \mathrm{and} \quad e_i \equiv 0 \pmod{n_j} \quad \mathrm{for} ~ i \ne j

Because of this, combined with the multiplication rules allowed in congruences, one solution to the system of simultaneous congruences is:

 x = \sum_{i=1}^k a_i e_i.\!

For example, consider the problem of finding an integer x such that

x \equiv 2 \pmod{3}, \,\!
x \equiv 3 \pmod{4}, \,\!
x \equiv 1 \pmod{5}. \,\!

Using the extended Euclidean algorithm for 3 and 4×5 = 20, we find (−13) × 3 + 2 × 20 = 1, i.e. e1 = 40. Using the Euclidean algorithm for 4 and 3×5 = 15, we get (−11) × 4 + 3 × 15 = 1. Hence, e2 = 45. Finally, using the Euclidean algorithm for 5 and 3×4 = 12, we get 5 × 5 + (−2) × 12 = 1, meaning e3 = −24. A solution x is therefore 2 × 40 + 3 × 45 + 1 × (−24) = 191. All other solutions are congruent to 191 modulo 60, (3 × 4 × 5 = 60) which means that they are all congruent to 11 modulo 60.

NOTE: There are multiple implementations of the extended Euclidean algorithm which will yield different sets of e1, e2, and e3. These sets however will produce the same solution i.e. 11 modulo 60.


extended Enclidean algorithm

[edit] Informal formulation of the algorithm

Dividend Divisor Quotient Remainder
120 23 5 5
23 5 4 3
5 3 1 2
3 2 1 1
2 1 2 0

It is assumed that the reader is already familiar with .

To illustrate the extension of the Euclid's algorithm, consider the computation of gcd(120, 23), which is shown on the table on the left. Notice that the quotient in each division is recorded as well alongside the remainder.

In this case, the remainder in the fourth line (which is equal to 1) indicates that the gcd is 1; that is, 120 and 23 are coprime (also called relatively prime). For the sake of simplicity, the example chosen is a coprime pair; but the more general case of gcd other than 1 also works similarly.

There are two methods to proceed, both using the division algorithm, which will be discussed separately.

[edit] The iterative method

This method computes expressions of the form ri = axi + byi for the remainder in each step i of the Euclidean algorithm. Each modulus can be written in terms of the previous two remainders and their whole quotient as follows:

By substitution, this gives:

The first two values are the initial arguments to the algorithm:

r1 = a = a(1) + b(0)
r2 = b = a(0) + b(1)

The expression for the last non-zero remainder gives the desired results since this method computes every remainder in terms of a and b, as desired.

Example: Compute the GCD of 120 and 23.

The computation proceeds as follows:

Step Quotient Remainder Substitute Combine terms
1 120 120 = 120 * 1 + 23 * 0
2 23 23 = 120 * 0 + 23 * 1
3 5 5 = 120 - 23 * 5 5 = (120 * 1 + 23 * 0) - (120 * 0 + 23 * 1) * 5 5 = 120 * 1 + 23 * -5
4 4 3 = 23 - 5 * 4 3 = (120 * 0 + 23 * 1) - (120 * 1 + 23 * -5) * 4 3 = 120 * -4 + 23 * 21
5 1 2 = 5 - 3 * 1 2 = (120 * 1 + 23 * -5) - (120 * -4 + 23 * 21) * 1 2 = 120 * 5 + 23 * -26
6 1 1 = 3 - 2 * 1 1 = (120 * -4 + 23 * 21) - (120 * 5 + 23 * -26) * 1 1 = 120 * -9 + 23 * 47
7 2 0 End of algorithm

The last line reads 1 = −9×120 + 47×23, which is the required solution: x = −9 and y = 47.

This also means that −9 is the multiplicative inverse of 120 modulo 23, and that 47 is the multiplicative inverse of 23 modulo 120.

−9 × 120 ≡ 1 mod 23 and also 47 × 23 ≡ 1 mod 120.

[edit] The recursive method

This method attempts to solve the original equation directly, by reducing the dividend and divisor gradually, from the first line to the last line, which can then be substituted with trivial value and work backward to obtain the solution.

Consider the original equation:

120 x + 23 y = 1
(5×23+5) x + 23 y = 1
23 (5x+y) + 5 x = 1
...
1 a + 0 b = 1

Notice that the equation remains unchanged after decomposing the original dividend in terms of the divisor plus a remainder, and then regrouping terms. If we have a solution to the equation in the second line, then we can work backward to find x and y as required. Although we don't have the solution yet to the second line, notice how the magnitude of the terms decreased (120 and 23 to 23 and 5). Hence, if we keep applying this, eventually we'll reach the last line, which obviously has (1,0) as a trivial solution. Then we can work backward and gradually find out x and y.

Dividend = Quotient x Divisor + Remainder
120 = 5 x 23 + 5
23 = 4 x 5 + 3
...

For the purpose of explaining this method, the full working will not be shown. Instead some of the repeating steps will be described to demonstrate the principle behind this method.

Start by rewriting each line from the first table with division algorithm, focusing on the dividend this time (because we'll be substituting the dividend).

120 x0 + 23 y0 = 1
(5×23+5) x0 + 23 y0 = 1
23 (5x0+y0) + 5 x0 = 1
23 x1 + 5 y1 = 1
(4×5+3) x1 + 5 y1 = 1
5 (4x1+y1) + 3 x1 = 1
5 x2 + 3 y2 = 1
  1. Assume that we were given x2=2 and y2=-3 already, which is indeed a valid solution.
  2. x1=y2=-3
  3. Solve 4x1+y1=x2 by substituting x1=-3, which gives y1=2-4(-3)=14
  4. x0=y1=14
  5. Solve 5x0+y0=x1 by substituting x0=14, so y0=-3-5(14)=-73


[edit] The table method

The table method is probably the simplest method to carry out with a pencil and paper. It is similar to the recursive method, although it does not directly require algebra to use and only requires working in one direction. The main idea is to think of the equation chain as a sequence of divisors . In the running example we have the sequence 120, 23, 5, 3, 2, 1. Any element in this chain can be written as a linear combination of the original x and y, most notably, the last element, gcd(x,y), can be written in this way. The table method involves keeping a table of each divisor, written as a linear combination. The algorithm starts with the table as follows:

a b d
1 0 120
0 1 23

The elements in the d column of the table will be the divisors in the sequence. Each di can be represented as the linear combination . The a and b values are obvious for the first two rows of the table, which represent x and y themselves. To compute di for any i > 2, notice that . Suppose . Then it must be that and . This is easy to verify algebraically with a simple substitution.

Actually carrying out the table method though is simpler than the above equations would indicate. To find the third row of the table in the example, just notice that 120 divided by 23 goes 5 times plus a remainder. This gives us k, the multiplying factor for this row. Now, each value in the table is the value two rows above it, minus k times the value immediately above it. This correctly leads to , , and . After repeating this method to find each line of the table (note that the remainder written in the table and the multiplying factor are two different numbers!), the final values for a and b will solve :

a b d
1 0 120
0 1 23
1 -5 5
-4 21 3
5 -26 2
-9 47 1

This method is simple, requiring only the repeated application of one rule, and leaves the answer in the final row of the table with no backtracking. Note also that if you end up with a negative number as the answer for the factor of, in this case b, you will then need to add the modulus in order to make it work as a modular inverse (instead of just taking the absolute value of b). I.e. if it returns a negative number, don't just flip the sign, but add in the other number to make it work. Otherwise it will give you the modular inverse yielding negative one.




xan 2008-09-26 13:19 鍙戣〃璇勮
]]>
[杞琞LCShttp://www.tkk7.com/xan/archive/2008/06/16/208331.htmlxanxanMon, 16 Jun 2008 06:47:00 GMThttp://www.tkk7.com/xan/archive/2008/06/16/208331.htmlhttp://www.tkk7.com/xan/comments/208331.htmlhttp://www.tkk7.com/xan/archive/2008/06/16/208331.html#Feedback0http://www.tkk7.com/xan/comments/commentRss/208331.htmlhttp://www.tkk7.com/xan/services/trackbacks/208331.html鏈闀垮叕鍏卞瓙搴忓垪闂LCS

鍙傝冭В絳?/h3>

鍔ㄦ佽鍒掔畻娉曞彲鏈夋晥鍦拌В姝ら棶棰樸備笅闈㈡垜浠寜鐓у姩鎬佽鍒掔畻娉曡璁$殑鍚勪釜姝ラ鏉ヨ璁′竴涓В姝ら棶棰樼殑鏈夋晥綆楁硶銆?/p>

1.鏈闀垮叕鍏卞瓙搴忓垪鐨勭粨鏋?/h4>

瑙f渶闀垮叕鍏卞瓙搴忓垪闂鏃舵渶瀹規(guī)槗鎯沖埌鐨勭畻娉曟槸絀蜂婦鎼滅儲(chǔ)娉曪紝鍗沖X鐨勬瘡涓涓瓙搴忓垪錛屾鏌ュ畠鏄惁涔熸槸Y鐨勫瓙搴忓垪錛屼粠鑰岀‘瀹氬畠鏄惁涓篨鍜孻鐨勫叕鍏卞瓙搴忓垪錛屽茍 涓斿湪媯(gè)鏌ヨ繃紼嬩腑閫夊嚭鏈闀跨殑鍏叡瀛愬簭鍒椼俋鐨勬墍鏈夊瓙搴忓垪閮芥鏌ヨ繃鍚庡嵆鍙眰鍑篨鍜孻鐨勬渶闀垮叕鍏卞瓙搴忓垪銆俋鐨勪竴涓瓙搴忓垪鐩稿簲浜庝笅鏍囧簭鍒梴1, 2, …, m}鐨勪竴涓瓙搴忓垪錛屽洜姝わ紝X鍏辨湁2m涓笉鍚屽瓙搴忓垪錛屼粠鑰岀┓涓炬悳绱㈡硶闇瑕佹寚鏁版椂闂淬?/p>

浜嬪疄涓婏紝鏈闀垮叕鍏卞瓙搴忓垪闂?shù)篃鏈夋渶浼樺瓙緇撴瀯鎬ц川錛屽洜涓烘垜浠湁濡備笅瀹氱悊錛?/p>

瀹氱悊: LCS鐨勬渶浼樺瓙緇撴瀯鎬ц川

璁懼簭鍒梄=<x1, x2, …, xm>鍜孻=<y1, y2, …, yn>鐨勪竴涓渶闀垮叕鍏卞瓙搴忓垪Z=<z1, z2, …, zk>錛屽垯錛?/p>

  1. 鑻m=yn錛屽垯zk=xm=yn涓擹k-1鏄疿m-1鍜孻n-1鐨勬渶闀垮叕鍏卞瓙搴忓垪錛?
  2. 鑻m≠yn涓攝k≠xm 錛?/sub>鍒橺鏄疿m-1鍜孻鐨勬渶闀垮叕鍏卞瓙搴忓垪錛?
  3. 鑻m≠yn涓攝k≠yn 錛屽垯Z鏄疿鍜孻n-1鐨勬渶闀垮叕鍏卞瓙搴忓垪銆?

鍏朵腑Xm-1=<x1, x2, …, xm-1>錛孻n-1=<y1, y2, …, yn-1>錛孼k-1=<z1, z2, …, zk-1>銆?/p>

璇佹槑

  1. 鐢ㄥ弽璇佹硶銆傝嫢zk≠xm錛屽垯<z1, z2, …, zk ,xm >鏄疿鍜孻鐨勯暱搴︿負(fù)k鍗?鐨勫叕鍏卞瓙搴忓垪銆傝繖涓嶼鏄疿鍜孻鐨勪竴涓渶闀垮叕鍏卞瓙搴忓垪鐭涚浘銆傚洜姝わ紝蹇呮湁zk=xm=yn銆傜敱姝ゅ彲鐭k-1鏄疿m-1鍜孻n-1鐨勪竴涓暱搴︿負(fù)k-1鐨勫叕鍏卞瓙搴忓垪銆傝嫢Xm-1鍜孻n-1鏈変竴涓暱搴﹀ぇ浜巏-1鐨勫叕鍏卞瓙搴忓垪W錛屽垯灝唜m鍔犲湪鍏跺熬閮ㄥ皢浜х敓X鍜孻鐨勪竴涓暱搴﹀ぇ浜巏鐨勫叕鍏卞瓙搴忓垪銆傛涓虹煕鐩俱傛晠Zk-1鏄疿m-1鍜孻n-1鐨勪竴涓渶闀垮叕鍏卞瓙搴忓垪銆?
  2. 鐢變簬zk≠xm錛孼鏄疿m-1鍜孻鐨勪竴涓叕鍏卞瓙搴忓垪銆傝嫢Xm-1鍜孻鏈変竴涓暱搴﹀ぇ浜巏鐨勫叕鍏卞瓙搴忓垪W錛屽垯W涔熸槸X鍜孻鐨勪竴涓暱搴﹀ぇ浜巏鐨勫叕鍏卞瓙搴忓垪銆傝繖涓嶼鏄疿鍜孻鐨勪竴涓渶闀垮叕鍏卞瓙搴忓垪鐭涚浘銆傜敱姝ゅ嵆鐭鏄疿m-1鍜孻鐨勪竴涓渶闀垮叕鍏卞瓙搴忓垪銆?
  3. 涓?2.綾諱技銆?

榪欎釜瀹氱悊鍛婅瘔鎴戜滑錛屼袱涓簭鍒楃殑鏈闀垮叕鍏卞瓙搴忓垪鍖呭惈浜?jiǎn)杩欎袱涓簭鍒楃殑鍓嵕~鐨勬渶闀垮叕鍏卞瓙搴忓垪銆傚洜姝わ紝鏈闀垮叕鍏卞瓙搴忓垪闂鍏鋒湁鏈浼樺瓙緇撴瀯鎬ц川銆?/p>

2.瀛愰棶棰樼殑閫掑綊緇撴瀯

鐢辨渶闀垮叕鍏卞瓙搴忓垪闂鐨勬渶浼樺瓙緇撴瀯鎬ц川鍙煡錛岃鎵懼嚭X=<x1, x2, …, xm>鍜孻=<y1, y2, …, yn>鐨勬渶闀垮叕鍏卞瓙搴忓垪錛屽彲鎸変互涓嬫柟寮忛掑綊鍦拌繘琛岋細(xì)褰搙m=yn鏃訛紝鎵懼嚭Xm-1鍜孻n-1鐨勬渶闀垮叕鍏卞瓙搴忓垪錛岀劧鍚庡湪鍏跺熬閮ㄥ姞涓妜m(=yn)鍗沖彲寰梄鍜孻鐨勪竴涓渶闀垮叕鍏卞瓙搴忓垪銆傚綋xm≠yn鏃訛紝蹇呴』瑙d袱涓瓙闂錛屽嵆鎵懼嚭Xm-1鍜孻鐨勪竴涓渶闀垮叕鍏卞瓙搴忓垪鍙?qiáng)X鍜孻n-1鐨勪竴涓渶闀垮叕鍏卞瓙搴忓垪銆傝繖涓や釜鍏叡瀛愬簭鍒椾腑杈冮暱鑰呭嵆涓篨鍜孻鐨勪竴涓渶闀垮叕鍏卞瓙搴忓垪銆?/p>

鐢辨閫掑綊緇撴瀯瀹規(guī)槗鐪嬪埌鏈闀垮叕鍏卞瓙搴忓垪闂鍏鋒湁瀛愰棶棰橀噸鍙犳ц川銆備緥濡傦紝鍦ㄨ綆梄鍜孻鐨勬渶闀垮叕鍏卞瓙搴忓垪鏃訛紝鍙兘瑕佽綆楀嚭X鍜孻n-1鍙?qiáng)Xm-1鍜孻鐨勬渶闀垮叕鍏卞瓙搴忓垪銆傝岃繖涓や釜瀛愰棶棰橀兘鍖呭惈涓涓叕鍏卞瓙闂錛屽嵆璁$畻Xm-1鍜孻n-1鐨勬渶闀垮叕鍏卞瓙搴忓垪銆?/p>

涓庣煩闃佃繛涔樼Н鏈浼樿綆楁搴忛棶棰樼被浼鹼紝鎴戜滑鏉ュ緩绔嬪瓙闂鐨勬渶浼樺肩殑閫掑綊鍏崇郴銆傜敤c[i,j]璁板綍搴忓垪Xi鍜孻j鐨勬渶闀垮叕鍏卞瓙搴忓垪鐨勯暱搴︺傚叾涓璛i=<x1, x2, …, xi>錛孻j=<y1, y2, …, yj>銆傚綋i=0鎴杍=0鏃訛紝絀哄簭鍒楁槸Xi鍜孻j鐨勬渶闀垮叕鍏卞瓙搴忓垪錛屾晠c[i,j]=0銆傚叾浠栨儏鍐典笅錛岀敱瀹氱悊鍙緩绔嬮掑綊鍏崇郴濡備笅錛?/p>

 

3.璁$畻鏈浼樺?/h4>

鐩存帴鍒╃敤(2.2)寮忓鏄撳啓鍑轟竴涓綆梒[i,j]鐨勯掑綊綆楁硶錛屼絾鍏惰綆楁椂闂存槸闅忚緭鍏ラ暱搴︽寚鏁板闀跨殑銆傜敱浜庡湪鎵鑰冭檻鐨勫瓙闂絀洪棿涓紝鎬誨叡鍙湁θ(m*n)涓笉鍚岀殑瀛愰棶棰橈紝鍥犳錛岀敤鍔ㄦ佽鍒掔畻娉曡嚜搴曞悜涓婂湴璁$畻鏈浼樺艱兘鎻愰珮綆楁硶鐨勬晥鐜囥?/p>

璁$畻鏈闀垮叕鍏卞瓙搴忓垪闀垮害鐨勫姩鎬佽鍒掔畻娉昄CS_LENGTH(X,Y)浠ュ簭鍒梄=<x1, x2, …, xm>鍜孻=<y1, y2, …, yn>浣滀負(fù)杈撳叆銆傝緭鍑轟袱涓暟緇刢[0..m ,0..n]鍜宐[1..m ,1..n]銆傚叾涓璫[i,j]瀛樺偍Xi涓嶻j鐨勬渶闀垮叕鍏卞瓙搴忓垪鐨勯暱搴︼紝b[i,j]璁板綍鎸囩ずc[i,j]鐨勫兼槸鐢卞摢涓涓瓙闂鐨勮В杈懼埌鐨勶紝榪欏湪鏋勯犳渶闀垮叕鍏卞瓙搴忓垪鏃惰鐢ㄥ埌銆傛渶鍚庯紝X鍜孻鐨勬渶闀垮叕鍏卞瓙搴忓垪鐨勯暱搴﹁褰曚簬c[m,n]涓?/p>

Procedure LCS_LENGTH(X,Y);
begin
m:=length[X];
n:=length[Y];
for i:=1 to m do c[i,j]:=0;
for j:=1 to n do c[0,j]:=0;
for i:=1 to m do
for j:=1 to n do
if x[i]=y[j] then
begin
c[i,j]:=c[i-1,j-1]+1;
b[i,j]:="鈫?;
end
else if c[i-1,j]≥c[i,j-1] then
begin
c[i,j]:=c[i-1,j];
b[i,j]:="↑";
end
else
begin
c[i,j]:=c[i,j-1];
b[i,j]:="←"
end;
return(c,b);
end;

鐢變簬姣忎釜鏁扮粍鍗曞厓鐨勮綆楄楄垂Ο(1)鏃墮棿錛岀畻娉昄CS_LENGTH鑰楁椂Ο(mn)銆?/p>

4.鏋勯犳渶闀垮叕鍏卞瓙搴忓垪

鐢辯畻娉昄CS_LENGTH璁$畻寰楀埌鐨勬暟緇刡鍙敤浜庡揩閫熸瀯閫犲簭鍒梄=<x1, x2, …, xm>鍜孻=<y1, y2, …, yn>鐨勬渶闀垮叕鍏卞瓙搴忓垪銆傞鍏堜粠b[m,n]寮濮嬶紝娌跨潃鍏朵腑鐨勭澶存墍鎸囩殑鏂瑰悜鍦ㄦ暟緇刡涓悳绱€傚綋b[i,j]涓亣鍒?鈫?鏃訛紝琛ㄧずXi涓嶻j鐨勬渶闀垮叕鍏卞瓙搴忓垪鏄敱Xi-1涓嶻j-1鐨勬渶闀垮叕鍏卞瓙搴忓垪鍦ㄥ熬閮ㄥ姞涓妜i寰楀埌鐨勫瓙搴忓垪錛涘綋b[i,j]涓亣鍒?↑"鏃訛紝琛ㄧずXi涓嶻j鐨勬渶闀垮叕鍏卞瓙搴忓垪鍜孹i-1涓嶻j鐨勬渶闀垮叕鍏卞瓙搴忓垪鐩稿悓錛涘綋b[i,j]涓亣鍒?←"鏃訛紝琛ㄧずXi涓嶻j鐨勬渶闀垮叕鍏卞瓙搴忓垪鍜孹i涓嶻j-1鐨勬渶闀垮叕鍏卞瓙搴忓垪鐩稿悓銆?/p>

涓嬮潰鐨勭畻娉昄CS(b,X,i,j)瀹炵幇鏍規(guī)嵁b鐨勫唴瀹規(guī)墦鍗板嚭Xi涓嶻j鐨勬渶闀垮叕鍏卞瓙搴忓垪銆傞氳繃綆楁硶鐨勮皟鐢↙CS(b,X,length[X],length[Y])錛屼究鍙墦鍗板嚭搴忓垪X鍜孻鐨勬渶闀垮叕鍏卞瓙搴忓垪銆?/p>

Procedure LCS(b,X,i,j);
begin
if i=0 or j=0 then return;
if b[i,j]="鈫? then
begin
LCS(b,X,i-1,j-1);
print(x[i]); {鎵撳嵃x[i]}
end
else if b[i,j]="↑" then LCS(b,X,i-1,j)
else LCS(b,X,i,j-1);
end;

鍦ㄧ畻娉昄CS涓紝姣忎竴嬈$殑閫掑綊璋冪敤浣縤鎴杍鍑?錛屽洜姝ょ畻娉曠殑璁$畻鏃墮棿涓?em>O(m+n)銆?/p>

渚嬪錛岃鎵緇欑殑涓や釜搴忓垪涓篨=<A錛孊錛孋錛孊錛孌錛孉錛孊>鍜孻=<B錛孌錛孋錛孉錛孊錛孉>銆傜敱綆楁硶LCS_LENGTH鍜孡CS璁$畻鍑虹殑緇撴灉濡傚浘2鎵紺恒?/p>


j
0
1
2
3
4
5
6
i

yj
B
D
C
A
B
A


鈹?/td> 鈹?/td>
0 xi 鈹?/td>

0
0
0
0
0
0 鈹?/td>


鈹?/td>



鈫?/td>


鈫?/td>
鈹?/td>
1 A 鈹?/td> 0
0
0
0
1 1
1 鈹?/td>


鈹?/td>
鈫?/td>





鈫?/td>


鈹?/td>
2 B 鈹?/td> 0
1 1 1
1
2 2 鈹?/td>


鈹?/td>


鈫?/td>




鈹?/td>
3 C 鈹?/td> 0
1
1
2 2
2
2 鈹?/td>


鈹?/td>
鈫?/td>



鈫?/td>


鈹?/td>
4 B 鈹?/td> 0
1
1
2
2
3 3 鈹?/td>


鈹?/td>

鈫?/td>




鈹?/td>
5 D 鈹?/td> 0
1
2
2
2
3
3 鈹?/td>


鈹?/td>



鈫?/td>

鈫?/td>
鈹?/td>
6 A 鈹?/td> 0
1
2
2
3
3
4 鈹?/td>


鈹?/td>
鈫?/td>



鈫?/td>

鈹?/td>
7 B 鈹?/td> 0
1
2
2
3
4
5 鈹?/td>


鈹?/td> 鈹?/td>

鍥?   綆楁硶LCS鐨勮綆楃粨鏋?/font>

5.綆楁硶鐨勬敼榪?/h4>

瀵逛簬涓涓叿浣撻棶棰橈紝鎸夌収涓鑸殑綆楁硶璁捐絳栫暐璁捐鍑虹殑綆楁硶錛屽線寰鍦ㄧ畻娉曠殑鏃墮棿鍜岀┖闂撮渶姹備笂榪樺彲浠ユ敼榪涖傝繖縐嶆敼榪涳紝閫氬父鏄埄鐢ㄥ叿浣撻棶棰樼殑涓浜涚壒孌婃с?/p>

渚嬪錛屽湪綆楁硶LCS_LENGTH鍜孡CS涓紝鍙繘涓姝ュ皢鏁扮粍b鐪佸幓銆備簨瀹炰笂錛屾暟緇勫厓绱燾[i,j]鐨勫間粎鐢眂[i-1,j-1]錛宑[i-1, j]鍜宑[i,j-1]涓変釜鍊間箣涓紜畾錛岃屾暟緇勫厓绱燽[i,j]涔熷彧鏄敤鏉ユ寚紺篶[i,j]絀剁珶鐢卞摢涓肩‘瀹氥傚洜姝わ紝鍦ㄧ畻娉昄CS涓紝鎴戜滑鍙互涓嶅熷姪浜庢暟 緇刡鑰屽熷姪浜庢暟緇刢鏈韓涓存椂鍒ゆ柇c[i,j]鐨勫兼槸鐢眂[i-1,j-1]錛宑[i-1,j]鍜宑[i,j-1]涓摢涓涓暟鍊煎厓绱犳墍紜畾錛屼唬浠鋒槸Ο(1)鏃墮棿銆傛棦鐒禸瀵逛簬綆楁硶LCS涓嶆槸蹇呰鐨勶紝閭d箞綆楁硶LCS_LENGTH渚夸笉蹇呬繚瀛樺畠銆傝繖涓鏉ワ紝鍙妭鐪?em>θ(mn)鐨勭┖闂達(dá)紝鑰孡CS_LENGTH鍜孡CS鎵闇瑕佺殑鏃墮棿鍒嗗埆浠嶇劧鏄?em>Ο(mn)鍜?em>Ο(m+n)銆備笉榪囷紝鐢變簬鏁扮粍c浠嶉渶瑕?em>Ο(mn)鐨勭┖闂達(dá)紝鍥犳榪欓噷鎵浣滅殑鏀硅繘錛屽彧鏄湪絀洪棿澶嶆潅鎬х殑甯告暟鍥犲瓙涓婄殑鏀硅繘銆?/p>

鍙﹀錛屽鏋滃彧闇瑕佽綆楁渶闀垮叕鍏卞瓙搴忓垪鐨勯暱搴︼紝鍒欑畻娉曠殑絀洪棿闇姹傝繕鍙ぇ澶у噺灝戙備簨瀹炰笂錛屽湪璁$畻c[i,j]鏃訛紝鍙敤鍒版暟緇刢鐨勭i琛屽拰絎琲-1琛屻傚洜姝わ紝鍙鐢?琛岀殑鏁扮粍絀洪棿灝卞彲浠ヨ綆楀嚭鏈闀垮叕鍏卞瓙搴忓垪鐨勯暱搴︺傛洿榪涗竴姝ョ殑鍒嗘瀽榪樺彲灝嗙┖闂撮渶姹傚噺鑷砿in(m, n)銆?/p>

xan 2008-06-16 14:47 鍙戣〃璇勮
]]> 主站蜘蛛池模板: 1000部免费啪啪十八未年禁止观看 | 精品国产免费一区二区三区香蕉| 亚洲色偷偷综合亚洲AV伊人| yellow视频免费看| 精品无码专区亚洲| 亚洲人成影院在线无码观看| a毛片在线免费观看| 亚洲美女视频一区二区三区| 成人在线视频免费| 国产精品免费久久久久影院| 亚洲一本综合久久| 一级特黄录像视频免费| 亚洲色图国产精品| 免费国产在线观看| 6080午夜一级毛片免费看6080夜福利| 亚洲精品天堂在线观看| 国产成人精品日本亚洲专区61 | 国产亚洲精品线观看动态图| 久久久久高潮毛片免费全部播放 | 中文字幕亚洲免费无线观看日本| 国产精品久久久久久久久久免费| 国产一二三四区乱码免费| 亚洲人成www在线播放| 亚洲熟妇无码AV在线播放| 永久免费毛片手机版在线看| 久久久久久国产精品免费免费男同 | 午夜dj在线观看免费视频| 成人一a毛片免费视频| 成年人免费网站在线观看| 日韩精品视频免费观看| 在线日本高清免费不卡| 无码AV片在线观看免费| 37pao成人国产永久免费视频 | 日本免费电影一区| 亚洲男人第一无码aⅴ网站| 一本久到久久亚洲综合| 亚洲专区在线视频| 中文字幕亚洲精品资源网| 亚洲精品无码人妻无码| 曰批免费视频播放在线看片二| 中文字幕一区二区三区免费视频|