poj 2887 块状数组/线段树
2015-04-03 23:36
260 查看
Big String
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
Sample Output
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个点,一般还可以用线段树离线输出结果。先把所有的查询和插入都进来,倒叙模拟插入,用线段树更新和统计区间和。
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 5952 | Accepted: 1405 |
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; }
相关文章推荐
- Poj 2887 Big String(块状数组)
- 【POJ 2887】Big String(块状数组)
- poj 2887 块状数组
- POJ 2887 块状数组
- poj 2299 Ultra-QuickSort 【线段树 or 线段树+lazy or 树状数组 or 归并排序】 求逆序对
- POJ 2828——Buy Tickets(树状数组,线段树——逆序遍历)
- poj-3468 线段树和树状数组的区间更新及求和
- POJ 2887 Big String(块状链表)
- POJ 2828 Buy Tickets (树状数组 or 线段树)
- poj1195 Mobile phones 二维线段树和二维树状数组两种做法 树套树
- POJ-2155-Matrix(二维树状数组 & 二维线段树)
- POJ 2155——Matrix(树套树,二维树状数组,二维线段树)
- POJ 1804 逆序对问题【分治】【线段树】【树状数组】【平衡树】
- POJ-2887(块状链表)
- POJ 2352 Stars(树状数组 or 线段树)
- POJ 2887 Big String 线段树 离线处理
- POJ 2182 Lost Cows (树状数组 or 线段树)
- poj 2828 -- Buy Tickets ( 线段树 / 树状数组 )
- POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)
- poj 2828 Buy Tickets(树状数组 | 线段树)