• <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>

            coreBugZJ

            此 blog 已棄。

            AC's code , FZU 2011年3月月賽之 C, FZU 2012

            Problem 2012 AC's code

            Time Limit: 1000 mSec    Memory Limit : 32768 KB

            Problem Description

            AC is given two strings A and B, and then he wants to insert B into A. As we know, there are |A| + 1 possible ways to make the final string! For example, if A = “orz” and B = “p”, then AC may get four final strings: “porz”, “oprz”, “orpz”, “orzp”. However, these strings are not so beautiful, so AC sort them in lexicographic order, then he gets {“oprz”, “orpz”, “orzp”,”porz”}.

            Now AC wants to know the k-th string in the sorted strings! For example, if AC wants to know the first string in the sorted strings, the answer is “oprz”, the second is “orpz”, the third is “orzp”, and the 4-th is “porz”.

            Now you are given string A and B, and the number K, in this problem, AC guarantees that K is valid.

            Input

            The first line of the input data is an integer number T (T <= 1000), represent the number of test cases.

            For each case, three lines follow.

            The first line has one string indicates A. (Without any space in A)

            The second line has one string indicates B. (Without any space in B)

            The third line has one integer indicates K.

            (1 <= |A|, |B| <= 10000, 1 <= K <= |A| + 1)

            Output

            For each test case, output a single line “Case idx:” where idx is the test case number starts from 1. Then one line output the k-th string in the sorted string.

            Sample Input

            4
            orz
            p
            1
            orz
            p
            2
            orz
            p
            3
            orz
            p
            4

            Sample Output

            Case 1:
            oprz
            Case 2:
            orpz
            Case 3:
            orzp
            Case 4:
            porz



            分析:
            字符串 hash,求第 k 小元素。

            字符串hash 函數(shù)為
            hash ( s[ 0..n ] ) = ( s[0]*q^n + s[1]*q^(n-1) + ...... + s[n-1]*q^1 + s[n]*q^0 ) mod p,其中,p, q 為大質(zhì)數(shù),
            適當(dāng)預(yù)處理(見(jiàn)函數(shù)init )后,B 插入在 A 中任意位置所形成的字符串的任意前綴的hash 值都能 O(1) 計(jì)算出來(lái)(見(jiàn)函數(shù)hash)。

            求第 k 小元素可以使用修改的快速排序(見(jiàn)函數(shù)sort),其中要注意使用隨機(jī),否則超時(shí)。復(fù)雜度 O(n)。

            比較兩個(gè)字符串大小時(shí),只比較它們的最長(zhǎng)公共前綴的后一個(gè)字符(見(jiàn)函數(shù) cmp)。
            而最長(zhǎng)公共前綴通過(guò)二分其長(zhǎng)度,用hash值判斷是否相等,可以O(shè)(log(|A|+|B|) )得到(見(jiàn)函數(shù) cmp)。

            另一種做法是用后綴數(shù)組的。


            我的代碼:
              1#include <stdio.h>
              2#include <string.h>
              3#include <stdlib.h>
              4#include <time.h>
              5
              6typedef  long long  Lint;
              7
              8#define  PRIME  55566677
              9#define  HH     23456789
             10
             11#define  L  21009
             12
             13int la, lb, k;
             14char a[ L ], b[ L ], c[ L + L ];
             15Lint ha[ L ], hb[ L ], hh[ L ];
             16
             17void init() {
             18        int i;
             19        srand( time( 0 ) );
             20
             21        memset( ha, 0sizeof(ha) );
             22        ha[ 0 ] = a[ 0 ] % PRIME;
             23        for ( i = 1; a[ i ]; ++i ) {
             24                ha[ i ] = ( ha[ i - 1 ] * HH + a[ i ] ) % PRIME;
             25        }

             26        la = i;
             27
             28        memset( hb, 0sizeof(hb) );
             29        hb[ 0 ] = b[ 0 ] % PRIME;
             30        for ( i = 1; b[ i ]; ++i ) {
             31                hb[ i ] = ( hb[ i - 1 ] * HH + b[ i ] ) % PRIME;
             32        }

             33        lb = i;
             34
             35        hh[ 0 ] = 1;
             36        for ( i = 1; i < L; ++i ) {
             37                hh[ i ] = ( hh[ i -1 ] * HH ) % PRIME;
             38        }

             39}

             40
             41/*
             42insert b after a[ i ]
             43query hash of [0..j]
             44*/

             45Lint hash( int i, int j ) {
             46        if ( i < 0 ) {
             47                if ( j < lb ) {
             48                        return hb[ j ];
             49                }

             50                return ( hb[ lb-1 ] * hh[ j-lb+1 ] + ha[ j-lb ] ) % PRIME;
             51        }

             52        if ( j <= i ) {
             53                return ha[ j ];
             54        }

             55        if ( j <= i + lb ) {
             56                return ( ha[ i ] * hh[ j-i ] + hb[ j-i-1 ] ) % PRIME;
             57        }

             58        return ( ha[i]*hh[j-i] + hb[lb-1]*hh[j-i-lb] + (ha[j-lb]+PRIME-((ha[i]*hh[j-lb-i])%PRIME)) ) % PRIME;
             59}

             60
             61char getint i, int j ) {
             62        if ( i < 0 ) {
             63                if ( j < lb ) {
             64                        return b[ j ];
             65                }

             66                return a[ j-lb ];
             67        }

             68        if ( j <= i ) {
             69                return a[ j ];
             70        }

             71        if ( j <= i + lb ) {
             72                return b[ j-i-1 ];
             73        }

             74        return a[ j-lb ];
             75}

             76
             77int cmp( int i, int j ) {
             78        int low = 0, high = la+lb-1, mid, lcp=-1;
             79        while ( low <= high ) {
             80                mid = ( low + high ) / 2;
             81                if ( hash(i,mid)==hash(j,mid) ) {
             82                        low = mid + 1;
             83                        if ( lcp < mid ) {
             84                                lcp = mid;
             85                        }

             86                }

             87                else {
             88                        high = mid - 1;
             89                }

             90        }

             91        return get(i,lcp+1- get(j,lcp+1);
             92}

             93
             94int idx[ L ];
             95void sort( int le, int ri, int k ) {
             96        int i, j, x;
             97        if ( (le>=ri) || (k<1) ) {
             98                return;
             99        }

            100        i = le + ( rand() % ( ri - le + 1 ) );
            101        x = idx[ i ];
            102        idx[ i ] = idx[ le ];
            103        idx[ le ] = x;
            104        i = le;
            105        j = ri;
            106        while ( i < j ) {
            107                while ( (i<j) && (cmp(x,idx[j])<=0) ) {
            108                        --j;
            109                }

            110                if ( i < j ) {
            111                        idx[ i++ ] = idx[ j ];
            112                }

            113                while ( (i<j) && (cmp(idx[i],x)<=0) ) {
            114                        ++i;
            115                }

            116                if ( i < j ) {
            117                        idx[ j-- ] = idx[ i ];
            118                }

            119        }

            120        idx[ i ] = x;
            121        if ( i - le + 1 < k ) {
            122                sort( i+1, ri, k-(i-le+1) );
            123        }

            124        else if ( i - le + 1 > k ) {
            125                sort( le, i-1, k );
            126        }

            127}

            128
            129char* solve() {
            130        int i, p;
            131        char tmp;
            132        for ( i = 0; i <= la; ++i ) {
            133                idx[ i ] = i - 1;
            134        }

            135        sort( 0, la, k );
            136        c[ 0 ] = 0;
            137        p = idx[ k - 1 ];
            138        tmp = a[ p + 1 ];
            139        a[ p + 1 ] = 0;
            140        strcat( c, a );
            141        a[ p + 1 ] = tmp;
            142        strcat( c, b );
            143        strcat( c, a+p+1 );
            144        return (char*)c;
            145}

            146
            147int main() {
            148        int td, cd = 0;
            149        scanf( "%d"&td );
            150        while ( td-- > 0 ) {
            151                scanf( "%s%s%d", a, b, &k );
            152                init();
            153                printf( "Case %d:\n%s\n"++cd, solve() );
            154        }

            155        return 0;
            156}

            157


            posted on 2011-03-21 21:12 coreBugZJ 閱讀(1892) 評(píng)論(9)  編輯 收藏 引用 所屬分類: ACM

            Feedback

            # re: AC's code , FZU 2011年3月月賽之 C, FZU 2012 2011-03-22 18:30 sodaisinie

            代碼無(wú)法打開(kāi)啊  回復(fù)  更多評(píng)論   

            # re: AC's code , FZU 2011年3月月賽之 C, FZU 2012 2011-03-22 18:52 coreBugZJ

            @sodaisinie
            原來(lái)是在ubuntu下用firefox編輯的。。。
            已經(jīng)win7下用IE重新編輯了  回復(fù)  更多評(píng)論   

            # re: AC's code , FZU 2011年3月月賽之 C, FZU 2012 2011-03-22 21:17 sodaisinie

            贊@coreBugZJ
              回復(fù)  更多評(píng)論   

            # re: AC's code , FZU 2011年3月月賽之 C, FZU 2012 2011-03-23 16:26 maoyu

            你好,請(qǐng)為什么這樣的hash就可以保證沒(méi)有重復(fù)值呢?萬(wàn)一出現(xiàn)兩種添加方式經(jīng)過(guò)hash后的值一樣,怎么辦呢?  回復(fù)  更多評(píng)論   

            # re: AC's code , FZU 2011年3月月賽之 C, FZU 2012 2011-03-23 19:48 maoyu

            還有一個(gè)疑問(wèn),這里用到的是大素?cái)?shù)modP,我之前開(kāi)了一個(gè)5位數(shù)的大素?cái)?shù),就WA了,然后把素?cái)?shù)換成你的那兩個(gè),就AC了。那到底要多大呢?  回復(fù)  更多評(píng)論   

            # re: AC's code , FZU 2011年3月月賽之 C, FZU 2012 2011-03-23 20:51 coreBugZJ

            @maoyu
            這個(gè)字符串 hash 從大空間映射到小空間,是有可能出現(xiàn)沖突的,只是輸入空間雖大,需要區(qū)別的數(shù)據(jù)量不是很大,沖突的概率還是比較小的,素?cái)?shù)開(kāi)的大一些,降低沖突的概率。。。
            PS:這是我第一次寫(xiě)字符串 hash,以前字典樹(shù)Trie用的比較多(對(duì)大量長(zhǎng)字符串沒(méi)轍 ^_^ )  回復(fù)  更多評(píng)論   

            # re: AC's code , FZU 2011年3月月賽之 C, FZU 2012 2011-03-24 17:45 maoyu

            我看福大核武的題解,也是這么說(shuō)。只是我個(gè)人覺(jué)得這題如果數(shù)據(jù)BT,需要兩個(gè)不同的狀態(tài)其hash值一定不同,我們能證明這種hash方法產(chǎn)生的每個(gè)狀態(tài)hash值都不一樣嗎?  回復(fù)  更多評(píng)論   

            # re: AC's code , FZU 2011年3月月賽之 C, FZU 2012 2011-03-24 17:56 coreBugZJ

            @maoyu
            我不能證明,比賽時(shí)我們就是卡在這一題和最后一題上,我是看了福大核武的題解過(guò)的。。。  回復(fù)  更多評(píng)論   

            # re: AC's code , FZU 2011年3月月賽之 C, FZU 2012 2011-03-26 17:30 Dragon521

            真的很贊  回復(fù)  更多評(píng)論   


            97久久婷婷五月综合色d啪蜜芽| 久久亚洲私人国产精品| 精品国产91久久久久久久a| 久久e热在这里只有国产中文精品99| 亚洲国产高清精品线久久 | 久久亚洲欧美日本精品| 久久www免费人成看国产片| 一本色综合网久久| 国产ww久久久久久久久久| 欧美精品国产综合久久| 亚洲国产精品久久| 久久久久久久97| 久久久噜噜噜久久中文字幕色伊伊 | 国产精品久久久久久五月尺| 久久精品人人槡人妻人人玩AV| 国产精品久久久久久久久久免费| 久久天天躁狠狠躁夜夜avapp| 久久精品国产亚洲欧美| 无码人妻精品一区二区三区久久久| 国产精品激情综合久久| 久久精品一本到99热免费| 亚洲日本va午夜中文字幕久久| 久久狠狠色狠狠色综合| 久久精品毛片免费观看| 欧美亚洲国产精品久久高清| 午夜欧美精品久久久久久久| 国产精品日韩深夜福利久久| 久久综合九色综合欧美就去吻| 亚洲αv久久久噜噜噜噜噜| Xx性欧美肥妇精品久久久久久| 亚洲国产成人久久综合一区77 | 日韩欧美亚洲综合久久影院Ds| 热re99久久6国产精品免费| 久久中文精品无码中文字幕| 亚洲日韩中文无码久久| 免费一级欧美大片久久网| 99热成人精品免费久久| 国产 亚洲 欧美 另类 久久| 亚洲国产成人久久精品影视| 久久99精品综合国产首页| 国产毛片久久久久久国产毛片|