• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            為生存而奔跑

               :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 330187
            • 排名 - 74

            最新評論

            閱讀排行榜

            評論排行榜

            題目大意:求最長的重復(fù)字串,要求:1.這兩個重復(fù)子串不重疊;2這兩個重復(fù)字串不要求相同,只要對應(yīng)的差值一樣即可。比如,字符串1 2 8 3 100 3 9 4, 1 2 8 和3 9 4就是符合條件的長度最長的。
            (1)要先把串轉(zhuǎn)換一下,根據(jù)題目的性質(zhì),應(yīng)該把輸入得到的
            串前后相減得到方便求解的新的串——設(shè)其為s,再求該串s中最長的
            不重疊重復(fù)子串。由于不能重疊,導(dǎo)致height數(shù)組的最大值不一定是
            解,因為相鄰兩串可能會重疊。
            (2) 二分答案,判斷是否存在滿足條件的長度為len的子串時,其實就是判斷height數(shù)組中是否存在連續(xù)的值大于等于len(這樣就保證了公共前綴長度大于等于len)并且他們之間的間隔大于等于len(這樣就保證了不重疊)。
            貼下鄙人的代碼
            ╭︿︿︿╮
            {/ ︿︿ /}
             ( (oo) )
             ︶ ︶ ︶
            ╭︿︿︿╮
            {/ ︿︿ /}
             ( (oo) )
             ︶ ︶ ︶
            ╭︿︿︿╮
            {/ ︿︿ /}
             ( (oo) )
             ︶ ︶ ︶
            #include<iostream>
            #include
            <cmath>
            #include
            <algorithm>
            using namespace std;
            #define maxn 20000+5
            char s[maxn];
            int sa[maxn],h[maxn],height[maxn],rank[maxn];
            int k,n;
            bool cmp1(const int & a,const int & b)
            {
                
            return (s[a]<s[b]);
            }

            bool cmp2(const int & a,const int & b)
            {
                
            return (rank[a]<rank[b] || (rank[a]==rank[b]&&
                    (a
            +k<n?rank[a+k]:-1)<(b+k<n?rank[b+k]:-1)));
            }

            void suffixArray()
            {
                
            int i,j;
                
            for(i=0;i<n;i++)
                    sa[i]
            =i;
                sort(sa,sa
            +n,cmp1);
                
            for(i=0;i<n;i++)
                
            {
                    
            if(i==0||s[sa[i]]!=s[sa[i-1]])
                        rank[sa[i]]
            =i;
                    
            else rank[sa[i]]=rank[sa[i-1]];
                }

                
            for(k=1;k<n;k*=2)
                
            {
                    sort(sa,sa
            +n,cmp2);
                    
            for(i=0;i<n;i++)
                    
            {
                        
            if( i==0 || (cmp2(sa[i],sa[i-1])||cmp2(sa[i-1],sa[i])) )
                            h[sa[i]]
            =i;
                        
            else h[sa[i]]=h[sa[i-1]];
                    }

                    memcpy(rank, h, n 
            * sizeof(int)); 
                }


                height[
            0= 0
                
            for(i = 0, j = 0; i < n; i++)   
                

                    
            if(rank[i]>0)
                    
            {
                        
            while(s[sa[rank[i] - 1+ j] == s[i + j])  
                            j
            ++
                        height[rank[i]] 
            = j; 
                        
            if(j > 0) j--
                    }

                 }

            }

            bool ok(int len)
            {
                
            int maxIndex=0,minIndex=INT_MAX;
                
            for(int i=1;i<n;i++)
                
            {    
                    
            if(height[i]<len)
                    
            {
                        maxIndex
            =0,minIndex=INT_MAX;
                    }

                    
            else
                    
            {
                        maxIndex
            =max(sa[i],maxIndex);
                        maxIndex
            =max(sa[i-1],maxIndex);
                        minIndex
            =min(sa[i],minIndex);
                        minIndex
            =min(sa[i-1],minIndex);
                        
            if(maxIndex-minIndex>=len)    
                            
            return 1;
                    }

                }

                
            return 0;
            }

            int binarySearch()
            {
                
            int low=0,up=n/2;
                
            int mid;
                
            while(low<up)
                
            {
                    mid
            =(low+up+1)>>1;
                    
            if(ok(mid))
                        low
            =mid;
                    
            else up=mid-1;
                }

                
            return low;
            }

            int main()
            {
                
            int i;
                
            while(scanf("%d",&n)!=EOF&&n)
                
            {
                    
            for(i=0;i<n;i++)
                        scanf(
            "%d",&s[i]);
                    
            for(i=1;i<n;i++)
                        s[i
            -1]=s[i]-s[i-1]+88;
                    s[n
            -1]=0;
                    suffixArray();
                    
            if(!ok(4))
                        printf(
            "0\n");
                    
            else
                        printf(
            "%d\n",binarySearch()+1);
                }

                
            return 0
            }
            posted on 2009-08-10 13:09 baby-fly 閱讀(515) 評論(0)  編輯 收藏 引用 所屬分類: Algorithm
            久久精品国产精品亚洲精品| 人妻丰满AV无码久久不卡| 久久中文字幕无码专区| 99国产欧美精品久久久蜜芽| 久久久久亚洲av成人网人人软件 | 99久久99久久精品免费看蜜桃| 久久婷婷国产综合精品| 国产三级精品久久| 日本免费久久久久久久网站| 色婷婷综合久久久久中文一区二区| .精品久久久麻豆国产精品| 亚洲精品tv久久久久| 理论片午午伦夜理片久久 | 亚洲综合熟女久久久30p| 午夜福利91久久福利| 天天做夜夜做久久做狠狠| 久久66热人妻偷产精品9| 精品久久8x国产免费观看| 色天使久久综合网天天| 久久精品人妻中文系列| 久久久WWW成人| 久久久久久久综合狠狠综合| 久久精品国产亚洲αv忘忧草| 91精品久久久久久无码| 久久综合伊人77777麻豆| 国产成人精品久久二区二区| 久久久久久国产精品免费无码 | 久久久久AV综合网成人 | 久久综合狠狠综合久久| 久久天天躁夜夜躁狠狠| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 久久久久久久久久久| 亚洲婷婷国产精品电影人久久| 久久乐国产精品亚洲综合| 精品久久久久久国产三级| 久久久久久国产a免费观看黄色大片| 伊人丁香狠狠色综合久久| 日本久久久精品中文字幕| 久久久久99精品成人片三人毛片 | 婷婷久久综合九色综合98| 久久精品成人免费网站|