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. windows下如何快速优雅的使用python的科学计算库?

    Python是一种强大的编程语言,其提供了很多用于科学计算的模块,常见的包括numpy.scipy.pandas和matplotlib.要利用Python进行科学计算,就需要一一安装所需的模块,而这些 ...

  2. 我眼中的Oracle Database Software 和 Oracle Database

    我眼中的Oracle Database Software 和 Oracle Database 我喜欢用微软的office软件和word文档(确切的说是:自己写的word文档,能够把这个Word文档想象 ...

  3. 【Python】IDLE启动错误

    启动IDLE时报Subprocess Startup Error错误 错误信息 IDLE's subprocess didn't make connection.Either IDLE cant't ...

  4. WPF中DATAGRID自定义验证(包含BINDINGGROUP)

    DataGrid在Wpf中的应用是十分广泛的,当你需要表中的信息稍详细的显示出来时,或者我们需要进行某些数据输入时,都有可能采用DataGrid.当然对信息的显示,我们不需要进行验证,但当我们将Dat ...

  5. Android中怎样控制LogCat的自己定义输出

    在Android开发中,LogCat是一个非常重要的调试工具,能够输出非常多关于项目或者手机的信息. 可是正是因为LogCat功能的过于强大,输出的信息量也是极为庞大的,那么我们就须要通过一定的方式依 ...

  6. TP框架---thinkphp模型

    1.获取系统常量信息的方法:在控制器DengLuController里面下写入下面的方法,然后调用该方法. public function test() { //echo "这是测试的&qu ...

  7. 【BZOJ1222】[HNOI2001]产品加工 DP

    [BZOJ1222][HNOI2001]产品加工 Description 某加工厂有A.B两台机器,来加工的产品可以由其中任何一台机器完成,或者两台机器共同完成.由于受到机器性能和产品特性的限制,不同 ...

  8. EasyNVR H5无插件摄像机直播解决方案前端解析之:关于直播页面和视频列表页面切换的问题

    关于直播页面和视频列表页面切换 为了给用户更好的用户体验,并且更好的让用户快速简洁的了解实时的视频直播信息.一般多会分为列表展示和实时的视频直播展示. 表面上只是两个视图之间的随意切换,其实切换的两个 ...

  9. lodash (js实用工具库)

    是什么? 它提供了一整套函数式编程的实用功能, 并且支持模块化, 比underscore更优秀. 文档? http://lodashjs.com/docs/ 引用? <script src=&q ...

  10. linux日志系统介绍 —— syslog(),openlog(),closelog()

    函数使用介绍 这里面的三个函数openlog, syslog.closelog是一套系统日志写入接口.另外那个vsyslog和syslog功能一样,仅仅是參数格式不同.         通常.sysl ...