POJ2887 Big String 块状链表

来源:互联网 发布:大鱼端口查看器下载 编辑:程序博客网 时间:2024/04/28 10:37

块状链表初体验 ╮(╯▽╰)╭~~

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define mxn 1000020#define N 1000#define N2 2003char s[mxn];struct node {char a[N2];int cnt;node() {cnt = 0;nxt = pre = NULL;}node *nxt, *pre;void split() {if( cnt < N2 - 3 )return ;node* tmp = new node;for( int i = cnt / 2 + 1; i <= cnt; ++i )tmp -> a[++tmp->cnt] = a[i];cnt /= 2;tmp -> pre = this;tmp -> nxt = nxt;nxt = tmp;}void insert( int k, char c ) {if( k > cnt ) {if( nxt == NULL ) {a[++cnt] = c;split();}else {nxt -> insert( k - cnt, c );}return ;}for( int i = cnt + 1; i > k + 1; --i )a[i] = a[i-1];a[k+1] = c;cnt++;split();}char get_k( int k ) {if( k > cnt )return nxt -> get_k( k - cnt );return a[k];}}*head;int main() {//freopen( "tt.txt", "r", stdin );while( scanf( "%s", s + 1 ) != EOF ) {head = new node;node* now = head;int tot = 0;for( int i = 1; s[i]; ++i ) {now -> a[++now->cnt] = s[i];if( i > tot + N ) {now -> nxt = new node;now -> nxt -> pre = now;now = now -> nxt;tot += N;}}int m;scanf( "%d", &m );while( m-- ) {char str[3];scanf( "%s", str );if( str[0] == 'Q' ) {int k;scanf( "%d", &k );printf( "%c\n", head -> get_k( k ) );continue;}int k;scanf( "%s %d", str, &k );k--;head -> insert( k, str[0] );}}return 0;}


0 0
原创粉丝点击