初识块状数组/块状链表(讲解+ 例题)

As we all known.数组定位的复杂度为O(1),插入删除的复杂度是O(n)

链表定位的复杂度是O(n),插入删除的复杂度是O(1)

块状数组结合了链表和数组的优点,使得所有操作的复杂度均为O(√n)

那么什么是块状数组呢?块状链表从宏观上看是链表,而链表中的每个节点又是一个数组。对于块状数组来说就是整个数据先分成若干个大数组,而这些大数组中又有小数组。再对其进行操作

典型范例:http://poj.org/problem?id=2887

题意:先给一行字符串。m次操作,Q表示查询第i(从1开始)个位置上的字符,I表示在第k个字母之前插入字母c

分析:显然暴力会超时,用线段树,树状数组,都要考虑新加入字母的问题。利用块状链表完美解决

代码:

//犯的错误:    int p = lower_bound(sum + 1,sum + 1+ num,pos) - sum;误把pos写成了p,导致无论如何都不对。
//块状数组模板
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
using namespace std;
int const maxn =  1000010;
int const smaxn = 2005;//smaxn = sqrt(maxn)
char ch[maxn];
int sum[maxn];
int num;//大块的个数
int n,m;
struct ListBlock//块状数组的每一个小块
{
    int size;//每个小块的大小
    char data[smaxn];
    void push_back(char c)
    {
        size ++;
        data[size] = c;
    }
    void insert(int pos,char c)
    {
        for(int i = size + 1; i > pos; i --)
        {
            data[i] = data[i - 1];
        }
        data[pos] = c;
        size ++;
    }
    char getData(int pos)
    {
        return data[pos];
    }
};
ListBlock block[smaxn];
void maintain()
{
    //此处size是直接调用了ListBlock中的变量size不是调用了系统的函数
    for(int i = 1; i <= num; i ++)
    {
        sum[i] = sum[i - 1] + block[i].size;
    }
}
void init()//n+m整个块状数组最大的大小.n初始状态块状数组的大小
{
    num = sqrt((n + m)*1.0) + 1;
    for(int i = 0; i < n; i ++)
    {
        block[i/num + 1].push_back(ch[i]);
    }
    maintain();
}
char query(int pos,int number)
{
    return block[number].getData(pos);
}
void insert(int pos,int number,char c)
{
    block[number].insert(pos,c);

}
char myQuery(int pos)
{
    int p = lower_bound(sum + 1,sum + 1+ num,pos) - sum;
    return   query(pos - sum[p - 1],p );

}
void myInsert(char c,int pos)
{
    int p = lower_bound(sum + 1,sum + 1+ num,pos) - sum;
    insert(pos - sum[p - 1] ,p,c);
    maintain();
}
int main()
{
  //  freopen("in.txt","r",stdin);
    scanf("%s",ch);
    n = strlen(ch);

    scanf("%d",&m);
    init();
    char s[3];
    char c[3];
    int p;
    for(int i = 0 ; i < m; i ++)
    {
        scanf("%s",s);
        if(s[0] == 'Q')
        {
            scanf("%d",&p);
            printf("%c\n",myQuery(p));
        }
        else
        {
            scanf("%s%d",c,&p);
            myInsert(c[0], p );
        }
    }
    return 0;
}


  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用指针来实现数组和链表之间的转换。具体来说,可以定义一个指向链表节点的指针,然后遍历数组中的元素,将每个元素插入到链表中。以下是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> struct ListNode { int val; struct ListNode *next; }; struct ListNode* arrayToList(int* nums, int size) { struct ListNode* head = NULL; struct ListNode* tail = NULL; for (int i = 0; i < size; i++) { struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode)); node->val = nums[i]; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } return head; } void printList(struct ListNode* head) { while (head != NULL) { printf("%d ", head->val); head = head->next; } printf("\n"); } int main() { int nums[] = {1, 2, 3, 4, 5}; int size = sizeof(nums) / sizeof(nums[0]); struct ListNode* head = arrayToList(nums, size); printList(head); return 0; } ``` 在上面的代码中,我们定义了一个结构体 `ListNode`,表示链表节点。然后定义了一个函数 `arrayToList`,用于将数组转换为链表。在函数中,我们遍历数组中的每个元素,创建一个新的节点,并将其插入到链表中。最后返回链表的头节点。我们还定义了一个函数 `printList`,用于打印链表中的所有元素。在 `main` 函数中,我们调用 `arrayToList` 函数将数组转换为链表,并调用 `printList` 函数打印链表中的所有元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值