POJ2887【块状链表】

参考自:
http://www.cnblogs.com/d-e-v-i-l/p/5721288.html
块状链表的应用:

字符串最长 Max_length=import_string_length+option_time; (输入字符串长度 + 操作数)
分成 (Max_Length) 个块,每个块字符串长度 (Max_Length) ,分割。

//一个比较好的定义:
struct BlockList
{
    int size;//该块字符串的长度
    char dat[2005];//该块字符串
    int at(int pos)//返回指定位置字符
    {
        return dat[pos];
    }
    void insert(int pos,char c)//在该块指定位置插入字符
    {
        for(int i=++size; i>pos; --i) dat[i]=dat[i-1];
        dat[pos]=c;
    }
    void push_back(char c)//往该块尾部插入字符
    {
        dat[++size]=c;
    }
};

void init()
{
    int len=strlen(str)+N;
    Maxn=sqrt(len*1.0)+1;
    for(int i=0; str[i]; ++i)
        block[i/Maxn+1].push_back(str[i]);
    maintain();
}

然后利用一个sum[] 数组统计长度,以便每次操作的时候可以二分找到在哪个块。
lower_bound(position);

void maintain()
{
    for(int i=1; i<=Maxn; ++i)
        sum[i]=sum[i-1]+block[i].size;
}
int p=lower_bound(sum+1,sum+1+Maxn,pos)-(sum);//寻找块位置。

对于查询操作,已经确定块位置,然后就是在块位置的字符位置,也简单啊。
那么复杂度其实就是 O(Log2(MaxLength)) ;

char query(int s,int p)
{
    return block[s].at(p);
}
int MyQuery(int pos)
{
    int p=lower_bound(sum+1,sum+1+Maxn,pos)-(sum);
    return query(p,pos-sum[p-1]);
}

那么,对于插入的话,同样是 O(Log2(Max_Length)) 得到块位置, O(Log2(Max_Length)) 更新字符(往后移),
然后指定位置插入字符就好了.

void Insert(int s,int p,int c){
    p = min(p,block[s].Size + 1);
    block[s].Insert(p,c);
}
void Myinsert(int pos,int c){
    int p = lower_bound(sum+1,sum+1+Maxn,pos) - (sum);
    Insert(p,pos-sum[p-1],c);//感受一下细节操作:pos-sum[p-1]
    maintain();
}

完整代码:

#include<iostream>
#include<cstdio>
#include<math.h>
#include<string>
#include<string.h>
#include<algorithm>
//#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL>PII;

int Maxn,N;
int sum[1005];
struct BlockList{
    int Size;
    char dat[2005];
    int atpos(int pos){
        return dat[pos];
    }
    void Insert(int pos,char c){
        for(int i=++Size;i > pos;i--) dat[i]=dat[i-1];
        dat[pos]=c;
    }
    void Push_back(char c){
        dat[++Size] = c;
    }
}block[1005];

char Query(int s,int p){
    return block[s].atpos(p);
}
void Insert(int s,int p,int c){
    p = min(p,block[s].Size + 1);
    block[s].Insert(p,c);
}
void maintain(){
    for(int i=1;i<=Maxn;++i){
        sum[i] = sum[i-1] + block[i].Size;
    }
}
void Myinsert(int pos,int c){
    int p = lower_bound(sum+1,sum+1+Maxn,pos) - (sum);
    Insert(p,pos-sum[p-1],c);
    maintain();
}
int Myquery(int pos){
    int p = lower_bound(sum+1,sum+1+Maxn,pos) - (sum);
    return Query(p,pos-sum[p-1]);
}
char str[1000005];
void init(){
    int len = strlen(str) + N;
    Maxn = sqrt(len*1.0) + 1;
    for(int i = 0;str[i];i++)
        block[i/Maxn+1].Push_back(str[i]);
    maintain();
}

int main(){
    gets(str);
    int pos;
    char p[3],s[3];
    scanf("%d",&N);
    init();
    while(N--){
        scanf("%s",p);
        if(p[0] == 'I'){
            scanf("%s%d",s,&pos);
            Myinsert(pos,s[0]);
        }
        else{
            scanf("%d",&pos);
            printf("%c\n",Myquery(pos));
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值