【POJ2887】【块状链表】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:
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 Sample Output a Source POJ Monthly--2006.07.30, zhucheng
|
【分析】
知识拿块状链表练练手而已。
这是我写的第一个块状链表,怎么说呢,块状链表应该说是写起来比较麻烦的,因为要注意的边界条件有点多,不过适应了应该会很好骗分。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <map> const int MAXC = + ;
const int MAXM = + ;
const int MAXN = + ;
const int N=, L=;//L代表单个块的长度,N为最大的总块数
using namespace std;
struct Block_List {//BLOCK_LIST为块状链表的英文名
struct Node {
char str[L];
//next数组志向下一个块状链表
int next, size;
void init(){
memset(str, , sizeof(str));
next = -;
size = ;
}
}list[N];
int head, tot; void init(char str[]){
head = tot = ;//整个块状链表进行初始化
list[tot++].init();//进行第一个块状链表的初始化
for (int i = , cur = head; str[i]; cur = list[cur].next){
for (int j = ; j < L && str[i]; j++, i++){
list[cur].str[j] = str[i];
list[cur].size++;
}
//还能继续装
if (str[i]){
list[tot].init();//注意tot永远指向下一个空的块状链表
list[cur].next = tot++;
}
}
for (int cur = head; cur != -; cur = list[cur].next)
if (list[cur].size == L) split(cur);//分割块状链表
}
//对第x块块状链表进行分割
void split(int x){
list[tot].init();
//注意块状链表的下标是从0 - (L - 1)
for (int i = L / ; i < L; i++){
list[tot].str[i - L/] = list[x].str[i];
list[tot].size++;
list[x].size--;
list[x].str[i] = ;//清空?好像没什么用
}
list[tot].next = list[x].next;
list[x].next = tot++;
}
void insert(int pos, char val){
int cur = head;
//注意开始不需要-1是因为一定成立,注意不要让任何一个块状链表达到满的状态,不然维护起来很麻烦
while (pos - list[cur].size > && list[cur].next != -){
pos -= list[cur].size;
cur = list[cur].next;
}
if (pos >= list[cur].size) list[cur].str[list[cur].size] = val;
else {
//先进行移动
for (int i = list[cur].size; i > pos; i--) list[cur].str[i] = list[cur].str[i - ] ;
list[cur].str[pos] = val;
}
list[cur].size++;
if (list[cur].size == L) split(cur);
}
char find(int pos){
int cur = head;
while ( pos - list[cur].size > ){
pos -= list[cur].size;
cur = list[cur].next;
}
return list[cur].str[pos - ];//注意要-1
}
}A;
char str[MAXN];
int n; int main() {
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
scanf("%s%d", str, &n);
A.init(str);//初始化块状链表
for (int i = ; i < n; i++){
int pos;
scanf("%s", str);
if (str[] == 'I'){//插入单个的单词
char S[];
scanf("%s%d", S, &pos);
A.insert(pos - , S[]);
} else {
scanf("%d", &pos);
printf("%c\n", A.find( pos ));
}
}
return ;
}
【POJ2887】【块状链表】Big String的更多相关文章
- POJ2887(块状链表)
Big String Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 6346 Accepted: 1525 Descr ...
- POJ 2887 Big String(块状链表)
题目大意 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 做法分析 好多不同的做法都可以 ...
- 【BZOJ1500】【块状链表】维修数列
Description Input 输入文件的第1行包含两个数N和M,N表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一条命令,格式参见问题描述 ...
- 【BZOJ2741】【块状链表+可持久化trie】FOTILE模拟赛L
Description FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max(Ai xor Ai+1 xor Ai+2 .. ...
- 【BZOJ3295】【块状链表+树状数组】动态逆序对
Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...
- 【HDU4391】【块状链表】Paint The Wall
Problem Description As a amateur artist, Xenocide loves painting the wall. The wall can be considere ...
- luogu P4008 [NOI2003]文本编辑器 splay 块状链表
LINK:文本编辑器 这个东西感觉块状链表写细节挺多 (块状链表本来就难写 解释一下块状链表的做法:其实是一个个数组块 然后利用链表给链接起来 每个块的大小为sqrt(n). 这样插入删除的时候直接暴 ...
- 【BZOJ-1507】Editor 块状链表
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 3397 Solved: 1360[Submit][Stat ...
- ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)
题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...
随机推荐
- KK的新书《必然》对未来科技趋势的预言
是他第一次在<失控>中提示我们-- 要用生物学而不是机械学的角度看待这个世界. 是他第一次在<科技想要什么>提示我们-- 科技本身就是一个生命体. 而在新书<必然 ...
- (转载)mysql书籍
(转载)http://blog.csdn.net/symdfbb/article/details/7636332 MySQL技术内幕 mysql使用大全,可以说方方面面都包括了.认真研读大概一本就差不 ...
- autoit使用
autoit下载地址: http://www.autoitx.com/thread-12964-1-1.html?sid=4zMMSb 在autoit的安装目录下有个Au3Info.exe文件,该文件 ...
- 锐浪应用小插曲,asp.net下的使用
下午提前完成了今天的工作内容,整了下bs中的应用,嘿嘿,其中遇到不少问题,接下来说下大概会遇到哪些问题,1:grid++ 6.0插件下载安装之后ie浏览器无法打开,居然什么都没有显示,奇葩啊,系统版本 ...
- js遍历数组和遍历对象的区别
<script> //----------------for用来遍历数组对象-- var i,myArr = [1,2,3]; for (var i = 0; i < myArr.l ...
- 将cocos的app直接在我的设备上测试运行
首先,你要有一个写好了的,准备在真机上测试的cocos程序. 1.设置ARC,设置的过程在另外一篇博文上有写. 2.在Target的Build Setting里面 找到Valid Archs 删除里面 ...
- JavaScript高级程序设计2.pdf
第三章 基本概念 区分大小写 ECMAScript中的一切(变量.函数名和操作符)都区分大小写 标识符 指变量.函数.属性的名字或者函数的参数 第一个字符必须是一个字母.下划线或美元符号,其它字符可以 ...
- Cogs 1672. [SPOJ375 QTREE]难存的情缘 LCT,树链剖分,填坑计划
题目:http://cojs.tk/cogs/problem/problem.php?pid=1672 1672. [SPOJ375 QTREE]难存的情缘 ★★★☆ 输入文件:qtree.in ...
- Myriad2 简介
本文翻译自英文: Myriad2图像处理器是一个永远在线的移动视觉处理器系统.它提供了非常强处理能力和及其高效了内存带宽以满足计算机视觉和计算成像应用的需求. Myriad2同时也满足移动设备的非常低 ...
- 如何使用TcpDump抓取远程主机的流量并回显到本地的WireShark上
ssh -t username@remoteip "echo rootpassword | sudo -S tcpdump -i eth0 -A '(tcp[((tcp[12:1] & ...