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 ;
}

最新文章

  1. 多点触摸(MT)协议(翻译)
  2. AC日记——过滤多余的空格 1.7 23
  3. 遍历windows驱动
  4. BizTalk开发系列(二十) 类型作用域
  5. C#获取本机的MAC地址
  6. 在Linux命令行窗口中,怎么向上翻页?
  7. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅲ
  8. BZOJ 1642: [Usaco2007 Nov]Milking Time 挤奶时间( dp )
  9. openGL研究钞四 : 关于颜色, 尺寸, 虚线, 多边形逆转, 空洞, 使用位图
  10. Java消息队列-Spring整合ActiveMq
  11. visual studio code 调试nodejs 配置简单HTTP服务器
  12. Tomcat 组件介绍
  13. tp5中设置指定的log日志,可单独建立文件夹和文件名
  14. JavaWeb项目架构之Kafka分布式日志队列
  15. 定时任务调度工作(学习记录 四)schedule与scheduleAtFixedRate的区别
  16. SpringBoot报错:nested exception is org.apache.ibatis.executor.ExecutorException: No constructor found in com.tuyrk.test.User matching [java.lang.Long, java.lang.String, java.lang.String]
  17. UE4中Component和Subobject的区别
  18. Mac idea 快捷键
  19. redis实现秒杀demo
  20. MovingBoxes左右滑动放大图片插件

热门文章

  1. C#高级编程八十一天----捕获异常
  2. WPF之DataGrid篇:DataGridComboBoxColumn
  3. iOS项目 -- 模仿花椒直播做的第三层设计完整版
  4. mysql批量插入测试数据
  5. python数据分析之Pandas:基本功能介绍
  6. Java语言基础(回头复习)
  7. PermissionError: [Errno 13] Permission denied:
  8. PAT 甲级 1116. Come on! Let's C (20) 【循环判断】
  9. 转载:SPFA算法学习
  10. CodeForces - 540D Bad Luck Island —— 求概率