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

【分析】

知识拿块状链表练练手而已。

这是我写的第一个块状链表,怎么说呢,块状链表应该说是写起来比较麻烦的,因为要注意的边界条件有点多,不过适应了应该会很好骗分。

 #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的更多相关文章

  1. POJ2887(块状链表)

    Big String Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 6346   Accepted: 1525 Descr ...

  2. POJ 2887 Big String(块状链表)

    题目大意 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 做法分析 好多不同的做法都可以 ...

  3. 【BZOJ1500】【块状链表】维修数列

    Description Input 输入文件的第1行包含两个数N和M,N表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一条命令,格式参见问题描述 ...

  4. 【BZOJ2741】【块状链表+可持久化trie】FOTILE模拟赛L

    Description FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max(Ai xor Ai+1 xor Ai+2 .. ...

  5. 【BZOJ3295】【块状链表+树状数组】动态逆序对

    Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...

  6. 【HDU4391】【块状链表】Paint The Wall

    Problem Description As a amateur artist, Xenocide loves painting the wall. The wall can be considere ...

  7. luogu P4008 [NOI2003]文本编辑器 splay 块状链表

    LINK:文本编辑器 这个东西感觉块状链表写细节挺多 (块状链表本来就难写 解释一下块状链表的做法:其实是一个个数组块 然后利用链表给链接起来 每个块的大小为sqrt(n). 这样插入删除的时候直接暴 ...

  8. 【BZOJ-1507】Editor 块状链表

    1507: [NOI2003]Editor Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 3397  Solved: 1360[Submit][Stat ...

  9. ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)

    题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...

随机推荐

  1. 【转】Ubuntu14.04搭建安装svnserver

    原文网址:http://www.cnblogs.com/blfshiye/p/5168028.html 前两天,公司准备搭建一个svnserver,供大家使用.于是.就先装了一个Ubuntu系统,然后 ...

  2. android滑动删除的多种实现方式(一)

    个人习惯,先上图 同事是个妹子(这点很重要),写滑动删除动能的时候用到了SwipeLayout,然后悲催的是,滑动时间被拦截了,解决方法先不提,在(一)中先讲解SwipeLayout下载listvie ...

  3. web页面布局思想

    一.盒子模型 网页可以看成由一个个"盒子"组成,如图: 由上图可以看出,页面分为上(网站导航).中.下(版权声明)三个部分,中间部分又分为左(商品分类).中(主要部分).右,这些版 ...

  4. windows下protobuf jar包的编译

    0.如果你不想手动编译生成,请直接跳到最后下载附件. 1.下载protobuf release版本:https://github.com/google/protobuf/releases,protoc ...

  5. poj 1847 Tram【spfa最短路】

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12005   Accepted: 4365 Description ...

  6. Java 8:不要再用循环了

    本文由 ImportNew - 进林 翻译自 deadcoderising.欢迎加入翻译小组.转载请见文末要求. 正如我之前所写的,Java 8中的新功能特性改变了游戏规则.对Java开发者来说这是一 ...

  7. FIREDAC TFDCONNECTION连接MYSQL数据库

    FIREDAC TFDCONNECTION连接MYSQL数据库 procedure TfrmDB.ConnectDB;begin FDMoniFlatFileClientLink1.FileName ...

  8. thread.wait的一个好例子

    int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); HelloThread thread; thread.star ...

  9. Redis Error

    1,MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. C ...

  10. android开发之Notification学习笔记

    今天总结了一下Notification的使用,与大家分享一下. MainActivity.java: 本文参考:http://www.jb51.net/article/36567.htm,http:/ ...