poj 2887 块状链表 单点插入+单点查询

块状链表第一题,懒得写成链表形式的,所以写成了静态链表。

思想还是很简单的,就是进行分块查找和插入,和建立索引有点像,复杂度是根号的,实现起来比较容易,由于这个题插入操作不多,所以没有写split函数,

因为没有删除操作,所以也没有写union函数,随后有空再补上吧。

  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <cmath>
  5 using namespace std;
  6 
  7 const int N = 1002001;
  8 const int M = 2000;
  9 char str[N];
 10 
 11 struct Block
 12 {
 13     char ch[M];
 14     int size;
 15     int next;
 16 
 17     void init()
 18     {
 19         size = 0;
 20         next = -1;
 21     }
 22 
 23 };
 24 
 25 struct BlockList
 26 {
 27     int head, len;    
 28     Block b[M];
 29 
 30     void init( char * s )
 31     {
 32         len = strlen(s);
 33         int as = sqrt( ( double ) len );
 34         head = 0;
 35         int j = head;
 36         b[j].init();
 37         for ( int i = 0; i < len; i++ )
 38         {
 39             if ( b[j].size == as )
 40             {
 41                 b[j].next = j + 1;
 42                 j++;                    
 43                 b[j].init();
 44             }
 45             b[j].ch[b[j].size++] = s[i];
 46         }
 47     }
 48 
 49     //插在第pos个元素前面
 50     void insert( int pos, char v )
 51     {
 52         if ( pos > len )
 53         {
 54             int i = head, j = b[head].next;
 55             while ( j != -1 )
 56             {
 57                 i = b[i].next;
 58                 j = b[j].next;
 59             }
 60             b[i].ch[b[i].size++] = v;
 61             len++;
 62             return ;
 63         }
 64         int i = head;        
 65         while ( pos > b[i].size )
 66         {
 67             pos -= b[i].size;
 68             i = b[i].next;
 69         }
 70         for ( int j = b[i].size; j >= pos; j-- )
 71         {
 72             b[i].ch[j] = b[i].ch[j - 1];
 73         }
 74         b[i].ch[pos - 1] = v;
 75         b[i].size++;
 76         len++;
 77         return ;
 78     }
 79 
 80     char at( int pos )
 81     {
 82         int i = head;
 83         while ( pos > b[i].size )
 84         {
 85             pos -= b[i].size;
 86             i = b[i].next;
 87         }
 88         return b[i].ch[pos - 1];
 89     }
 90 
 91 } bl;
 92 
 93 int main()
 94 {
 95     while ( scanf("%s", str) != EOF )
 96     {
 97         bl.init(str);
 98         int n;
 99         char op[2];        
100         scanf("%d", &n);
101         while ( n-- )
102         {
103             scanf("%s", op);
104             if ( op[0] == 'I' )
105             {
106                 char ch[2];                
107                 int p;
108                 scanf("%s%d", ch, &p);
109                 bl.insert( p, ch[0] );
110             }
111             else
112             {
113                 int p;
114                 scanf("%d", &p);
115                 char ans = bl.at(p);
116                 printf("%c
", ans);
117             }
118         }
119     }
120     return 0;
121 }
原文地址:https://www.cnblogs.com/huoxiayu/p/4461417.html