您的位置:首页 > 其它

poj 2887 块状数组/线段树

2015-04-03 23:36 260 查看
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:

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.
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

POJ Monthly--2006.07.30, zhucheng

字符串总长度不超过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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  块状数组 线段树