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

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
标准块状链表.
#include"cstdio"
#include"cstring"
#include"cmath"
#include"algorithm"
using namespace std;
const int MAXN=;
char str[MAXN*MAXN];
struct Block_List{
int size,next;
char s[*MAXN];
Block_List()
{
memset(s,,sizeof(s));
size=;
next=-;
} void push(char ch)
{
s[size++]=ch;
} void insert(int pos,char ch)
{
for(int i=size;i>pos;i--)
{
s[i]=s[i-];
}
s[pos]=ch;
size++;
}
}bkl[MAXN]; int bsize,cnt,m;
void Init()
{
gets(str);
scanf("%d",&m);
bsize=(int)sqrt(double(strlen(str)+m));
for(int i=;str[i];i++)
{
if(bkl[cnt].size==bsize)
{
bkl[cnt].next=cnt+;
cnt++;
}
bkl[cnt].push(str[i]);
}
cnt++;
} void Update(int u)
{
if(bkl[u].size<*bsize) return ;
/*
for(int i=bsize;i<bkl[u].size;i++)
{
bkl[cnt].push(bkl[u].s[i]);
}
*/
strcpy(bkl[cnt].s,bkl[u].s+bsize);
bkl[cnt].size=strlen(bkl[u].s)-bsize;
bkl[u].size=bsize;
bkl[cnt].next=bkl[u].next;
bkl[u].next=cnt;
cnt++;
}
void handle()
{
int cas=;
while(++cas<=m)
{
char op[];
scanf("%s",op);
if(op[]=='Q')
{
int pos;
scanf("%d",&pos);
int i;
for(i=;pos>bkl[i].size;i=bkl[i].next)
{
pos-=bkl[i].size;
}
printf("%c\n",bkl[i].s[pos-]);
}
else
{
char ch[];
int pos;
scanf("%s%d",ch,&pos);
int i;
for(i=;pos>bkl[i].size&&bkl[i].next!=-;i=bkl[i].next)
{
pos-=bkl[i].size;
}
bkl[i].insert(pos-,ch[]);
Update(i);
}
}
}
int main()
{
Init();
handle();
return ;
}

POJ2887(块状链表)的更多相关文章

  1. 【POJ2887】【块状链表】Big String

    Description You are given a string and supposed to do some string manipulations. Input The first lin ...

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

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

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

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

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

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

  5. 【BZOJ 1507】【NOI 2003】&【Tyvj P2388】Editor 块状链表模板题

    2016-06-18 当时关于块状链表的想法是错误的,之前维护的是一个动态的$\sqrt{n}$,所以常数巨大,今天才知道原因TwT,请不要参照这个程序为模板!!! 模板题水啊水~~~ 第一次写块状链 ...

  6. BZOJ 1507 Editor(块状链表)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1507 题意:一个文本编辑器,模拟以下操作: 思路:块状链表的主要操作: (1)find( ...

  7. bzoj 3809 Gty的二逼妹子序列(莫队算法,块状链表)

    [题意] 回答若干个询问,(l,r,a,b):区间[l,r]内权值在[a,b]的数有多少[种]. [思路] 考虑使用块状链表实现莫队算法中的插入与删除. 因为权值处于1..n之间,所以我们可以建一个基 ...

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

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

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

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

随机推荐

  1. 用汇编的角度剖析c++的virtual

    多态是c++的关键技术,背后的机制就是有一个虚函数表,那么这个虚函数表是如何存在的,又是如何工作的呢? 当然不用的编译器会有不同的实现机制,本文只剖析vs2015的实现. 单串继承 首先看一段简单的代 ...

  2. PHPExcel简易使用教程

    在企业里使用PHP进行开发,不可避免总会遇到读/写Excel的需求,遇到这种需求,一般使用PHPExcel类库进行开发. PHPExcel现在最新版本是1.8.0,最低需要PHP5.2版本,支持读取x ...

  3. git系列1

    git clone支持多种协议,除了HTTP(s)以外,还支持SSH.Git.本地文件协议等,下面是一些例子. $ git clone http[s]://example.com/path/to/re ...

  4. 百思不得姐之&quot;我的&quot;模块功能(六)

    一 功能图和知识点 1 功能图部分:(因为网速的原因,网页部分没有载入出来,可是功能完善) 2 该部分能学到的知识点概括: >1 UITableView的使用(简单) >2 UIColle ...

  5. JavaScript学习3:原型和继承

    原型 我们创建的每个函数都有一个prototype(原型)属性,这个属性是一个对象,它的用途是包括能够由特定类型的全部实例共享的属性和方法.逻辑上能够这么理解:prototype是通过调用构造函数而创 ...

  6. 用canvas合成图片

    朋友圈有些分享功能是通过长按图片另存来实现的,就像淘宝内部要分享朋友圈的时候一样,这些图片可以用canvas来合成. 获取了img的dom对象以后,进行base64的转. //加载对象$page.ge ...

  7. ifndef/define/endif 和 #ifdef 、#if 作用和用法

    为了能简单的看看某些linux内核源码,复习了一下c语音,今天汇总了一下关于宏定义的相关内容: 一.ifndef/define/endif用法: .h文件,如下: #ifndef XX_H #defi ...

  8. 使用服务端的临时密钥,不依赖阿里js的putFIle--》阿里oss

    <!DOCTYPE html> <html lang='en'> <head> <meta charset='UTF-8'> <title> ...

  9. stacked generalization 堆积正则化 堆积泛化 加权特征线性堆积

    https://en.wikipedia.org/wiki/Ensemble_learning Stacking Stacking (sometimes called stacked generali ...

  10. 流畅python学习笔记第十八章:使用asyncio包处理并发(二)

    前面介绍了asyncio的用法.下面我们来看下如何用协程的方式来实现之前的旋转指针的方法 @asyncio.coroutine def spin(msg): write,flush=sys.stdou ...