乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      編程珠璣 第一章 習(xí)題9

       Clay*more 2012-04-03

       題目:

      9. One problem with trading more space to use less time is that initializing the space can itself take a great deal of time. Show how to circumvent this problem by designing a technique to initialize an entry of a vector to zero the first time it is accessed. Your scheme should use constant time for initialization and for each vector access, and use extra space proportional to the size of the vector.

      Because this method reduces initialization time by using even more space, it should be considered only when space is cheap, time is dear and the vector is sparse.

       

      提供的參考答案:

      The effect of initializing the vector data[0..n-1] can be accomplished with a signature contained in two additional n-element vectors, from and to, and an integer, top. If the element data[i] has been initialized, then from [i] < top and to[from[i]] = i. Thus from is a simple signature ,and to and top together make sure that from is not accidentally signed by the random contents of memory.

      Blank entries of data are uninitialized in this picture:

              data :  |  |3 |  |2  |   | 8 |   |   |

              from:|  |0|   |2 |   | 1 |   |  |

              to:     |1|5|3|    |   |    |    |   |

       

      The variable top is initially zero; the array element i is first accessed by the code

              from[i] = top

              to[top]  = i

              data[i]  = 0

              top++

      This problem and solution are form Exercise 2.12 of Aho, Hopcroft and Ullman's Design and Analysis of Computer Algorithms. It combines key indexing and a wily signature scheme. It can be used for matrices as well as vectors.

       


      首先,我們需要明確問題是什么。

             在這里,我們有一個(gè)稀疏的數(shù)組需要訪問,并且在第一次訪問的時(shí)候?qū)⑵涑跏蓟癁?. 因?yàn)閿?shù)組很大,并且需要訪問的數(shù)組元素很稀疏,而程序要求的時(shí)間很寶貴。

             所以,我們不能直接將data數(shù)組的各個(gè)元素都初始化為0.

             我們需要做的是在第一次數(shù)組data中的某個(gè)元素的時(shí)候,將其初始化為0. 如果之后再次訪問到該元素,應(yīng)該能夠判斷其是否已經(jīng)被初始化過,避免多次初始化從而覆蓋數(shù)據(jù)。

             在提供的解決方案中,就使用了from,to兩個(gè)向量和top變量來保存哪些變量已經(jīng)被初始化了。

             當(dāng)我們訪問索引為i的data元素時(shí),我們通過判斷form[i]是否小于top,并且to[from[i]]是否等于i來判斷元素樹否被初始化過。

             需要注意的是我們使用to向量的原因是為了防止from中的未經(jīng)初始化的數(shù)據(jù)剛好因?yàn)樾∮趖op而導(dǎo)致出現(xiàn)錯(cuò)誤判斷。也就是說,我們是不對(duì)from和to向量進(jìn)行初始化的(因?yàn)槲覀冞B對(duì)data進(jìn)行初始化的成本都不肯),所以無法判斷from中的數(shù)據(jù)哪些是我們寫入的,哪些是原來就有的。于是,我們就通過增加一個(gè)to數(shù)組,并且增加一個(gè)判斷條件來保證我們的判斷是正確的。

             當(dāng)然,這樣的保證也并不是絕對(duì)正確的。只是小概率事件,直接忽略了吧???

      綜上:

             我們在訪問data中索引為i的元素時(shí),通過條件from[i]<top和to[from[i]]=i來判斷該數(shù)據(jù)是否被初始化過。

             如果已被初始化過,就直接訪問該數(shù)據(jù);

             否則,使用如下的語句對(duì)其進(jìn)行初始化,并且在from,to和top中保存該數(shù)據(jù)已被初始化過這個(gè)信息:

             from[i]=top;

             to[top]=i

            data[i] = 0

            top++.
       
      具體的應(yīng)用就是,先對(duì)一個(gè)大而且稀疏的數(shù)據(jù)進(jìn)行部分清零初始化,然后用to和from保證數(shù)組的那些元素是要用的,但是還不清楚具體的應(yīng)用時(shí)什么?
       

      反向理解(?。?data這稀疏數(shù)組,未經(jīng)過全數(shù)組的清0操作。這樣在從data中取值時(shí)不知道那些是有用的值。這樣需要一個(gè)驗(yàn)證條件(from[i]<top和to[from[i]]=i)來判斷。
      正向理解(存):每當(dāng)往data數(shù)值中放一個(gè)值時(shí),設(shè)置驗(yàn)證條件。
      from 和 to是否應(yīng)該為無符號(hào)整型,否則to[from[i]]將崩潰

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多