锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
浣嶆暟 [lgN!]+1=lg(N(N-1)...1)+1 = (lgN+lg(N-1)+...+lg1) + 1
]]>
loop:
1. 濡傛灉S涓厓绱犱釜鏁頒負(fù)0鎴栬?,榪斿洖
2. 鍙朣涓換鎰忓厓绱爒涓烘灑綰?br />
3. 灝哠涓綑涓嬪厓绱犳寜>v 鍜?<v鍒嗘垚涓や釜涓嶅悓閮ㄥ垎
4. 瀵硅繖涓や釜閮ㄥ垎蹇熸帓搴?br />
鏋㈢航鍏冮夋嫨:
涓鑸噰鐢⊿涓搗濮?緇撴潫,涓棿浣嶇疆鐨勪笁涓肩殑涓間負(fù)鏋㈢航鍏?(涓夋暟涓煎垎鍓叉硶)
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
Furthermore, all solutions x to this system are congruent modulo the product N = n1n2…nk.
Hence for all
, if and only if
.
Sometimes, the simultaneous congruences can be solved even if the ni's are not pairwise coprime. A solution x exists if and only if:
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).
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:
Again, to begin, the product 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:
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 .
Because of this, combined with the multiplication rules allowed in congruences, one solution to the system of simultaneous congruences is:
For example, consider the problem of finding an integer x such that
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.
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.
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:
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.
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 |
|
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.
鍔ㄦ佽鍒掔畻娉曞彲鏈夋晥鍦拌В姝ら棶棰樸備笅闈㈡垜浠寜鐓у姩鎬佽鍒掔畻娉曡璁$殑鍚勪釜姝ラ鏉ヨ璁′竴涓В姝ら棶棰樼殑鏈夋晥綆楁硶銆?/p>
瑙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>
- 鑻m=yn錛屽垯zk=xm=yn涓擹k-1鏄疿m-1鍜孻n-1鐨勬渶闀垮叕鍏卞瓙搴忓垪錛?
- 鑻m≠yn涓攝k≠xm 錛?/sub>鍒橺鏄疿m-1鍜孻鐨勬渶闀垮叕鍏卞瓙搴忓垪錛?
- 鑻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>
璇佹槑
榪欎釜瀹氱悊鍛婅瘔鎴戜滑錛屼袱涓簭鍒楃殑鏈闀垮叕鍏卞瓙搴忓垪鍖呭惈浜?jiǎn)杩欎袱涓簭鍒楃殑鍓嵕~鐨勬渶闀垮叕鍏卞瓙搴忓垪銆傚洜姝わ紝鏈闀垮叕鍏卞瓙搴忓垪闂鍏鋒湁鏈浼樺瓙緇撴瀯鎬ц川銆?/p>
鐢辨渶闀垮叕鍏卞瓙搴忓垪闂鐨勬渶浼樺瓙緇撴瀯鎬ц川鍙煡錛岃鎵懼嚭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>
鐩存帴鍒╃敤(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>
鐢辯畻娉昄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>
瀵逛簬涓涓叿浣撻棶棰橈紝鎸夌収涓鑸殑綆楁硶璁捐絳栫暐璁捐鍑虹殑綆楁硶錛屽線寰鍦ㄧ畻娉曠殑鏃墮棿鍜岀┖闂撮渶姹備笂榪樺彲浠ユ敼榪涖傝繖縐嶆敼榪涳紝閫氬父鏄埄鐢ㄥ叿浣撻棶棰樼殑涓浜涚壒孌婃с?/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>