poj 2887 块状数组/线段树

Big String
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 5952 Accepted: 1405

Description

You are given a string and supposed to do some string manipulations.

Input

The first line of the input contains the initial string. You can assume that it is non-empty and its length does not exceed 1,000,000.

The second line contains the number of manipulation commands N (0 < N  2,000). The following N lines describe a command each. The commands are in one of the two formats below:

  1. I ch p: Insert a character ch before the p-th character of the current string. If p is larger than the length of the string, the character is appended to the end of the string.
  2. Q p: Query the p-th character of the current string. The input ensures that the p-th character exists.

All characters in the input are digits or lowercase letters of the English alphabet.

Output

For each Q command output one line containing only the single character queried.

Sample Input

ab
7
Q 1
I c 2
I d 4
I e 2
Q 5
I f 1
Q 3

Sample Output

a
d
e

Source

字符串总长度不超过10^6,插入操作不超过2000次。可以用块状数组存储字符串,比如10^6分成1000行存,每行1000个,并用len[]数组记录每行的字符串长度。插入第k个就找到第k个字符应该属于哪一行,直接插入,操作次数也不超过改行字符串长度也就是几千。

块状数组思想是平方分割法,将长度为n的数组分sqrt(n)块,每个长度为n/sqrt(n)=sqrt(n),对每个区间进行操作只需要O(sqrt(n))的复杂度。

这题是插入和查询第k个点,一般还可以用线段树离线输出结果。先把所有的查询和插入都进来,倒叙模拟插入,用线段树更新和统计区间和。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

#define maxn 1001

char s[maxn*maxn];
char a[maxn][maxn*3]; //插入最多2000次,每行最多3000个
int len[maxn];  //记录每一行字符个数
const int b = 1000; //默认分成最多1000行
int row; //共几行

char query(int k) //查询第k个
{
    int cnt = 0;
    for(int i = 0; i < row; i++){
        if(cnt+len[i]>= k){
            return a[i][k-cnt-1];
        }
        cnt+=len[i];
    }
}

void add(char ch, int k) //插入到第k个
{
    int cnt = 0;
    int r;
    for(int i = 0; i < row; i++){
        if(cnt+len[i] >= k){
            r = i;
            break;
        }
        if(i == row-1) {r = row-1; break;}
        cnt+=len[i];
    }

    int pos = k-cnt-1;
    if(pos >= len[r]) pos = len[r];
    for(int i = len[r]; i >= pos+1; i--)
        a[r][i] = a[r][i-1];
    a[r][pos] = ch;
    len[r]++;

}

int main()
{
    while(scanf("%s", s)==1){
             memset(len, 0, sizeof(len));
            int slen = strlen(s);
            int perl = (slen+b-1)/b; //每行保存的字符个数

            row = (slen-1)/perl+1; //共多少行
            for(int i = 0; i < row; i++) len[i] = perl;
            for(int i = 0; i < slen; i++)
                a[i/perl][i%perl] = s[i];
            len[row-1] = (slen-1)%perl+1;

            int n;
            scanf("%d", &n);
            while(n--){
                char op[3];
                int k;

                scanf("%s", op);
                if(op[0] == 'Q'){
                    scanf("%d", &k);
                    printf("%c\n", query(k));
                }
                else{
                    scanf("%s %d", op, &k);
                    add(op[0], k);
                }
            }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值