uva 11988 塊狀鏈表

來源:互聯網
上載者:User

標籤:

其實鏈表就可以搞定,不過分析一下感覺塊鏈也可以過,就套模板試了一下,果然沒有讓我失望,看來我寫的塊鏈還是不錯的,哈哈。

 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5  6 const int M = 100001; 7 const int N = 500; 8 char text[M]; 9 char b[N][N];10 int sz[N];11 int next[N];12 int ps, bl;13 14 void init()15 {16     bl = 1;17     ps = 400;18     memset( sz, 0, sizeof(sz) );19     memset( next, -1, sizeof(next) );20 }21 22 void spilt( int cur )23 {24     int tmp = next[cur];25     int ncur = next[cur] = bl++;26     next[ncur] = tmp;27     for ( int i = sz[cur] / 2; i < sz[cur]; i++ )28     {29         b[ncur][sz[ncur]++] = b[cur][i];30     }31     sz[cur] = sz[cur] / 2;32 }33 34 void insert( int pos, char val )35 {36     int cur = 0, p = sz[cur];37     while ( p < pos + 1 && next[cur] != -1 )38     {39         cur = next[cur];40         p += sz[cur];41     }42     if ( p < pos + 1 )43     {44         b[cur][sz[cur]++] = val;45     }46     else47     {48         p -= sz[cur];49         pos -= p;50         for ( int j = sz[cur] - 1; j >= pos; j-- )51         {52             b[cur][j + 1] = b[cur][j];53         }54         b[cur][pos] = val;55         sz[cur]++;56     }57     if ( sz[cur] > ps ) spilt(cur);58 }59 60 int main ()61 {62     while ( gets(text) )63     {64         init();65         int ptr = 0, tot = 0;66         for ( int i = 0; text[i] != ‘\0‘; i++ )67         {68             if ( text[i] == ‘[‘ ) 69             {70                 ptr = 0;71             }72             else if ( text[i] == ‘]‘ )73             {74                 ptr = tot;75             }76             else77             {78                 insert( ptr++, text[i] );79                 tot++;80             }81         }82            for ( int i = 0; i != -1; i = next[i] )83            {84                for ( int j = 0; j < sz[i]; j++ )85                {86                    putchar( b[i][j] );87                }88            }89            putchar(‘\n‘);90     }91     return 0;92 }

 

uva 11988 塊狀鏈表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.