Big String

Time Limit: 1000MS Memory Limit: 131072K

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:

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.

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

POJ Monthly–2006.07.30, zhucheng

  1. /*
  2. 块状数组裸题.
  3. 把链表和数组的特性结合起来.
  4. 各个块互不影响(包括长度).
  5. 定位的时候看在哪一块就可以了.
  6. 各种复杂度都是√n.
  7. */
  8. #include<iostream>
  9. #include<cstdio>
  10. #include<cstring>
  11. #define MAXN 1000010
  12. #define MAXM 1001
  13. using namespace std;
  14. int n,m,k,ans,tot,len[MAXN];
  15. char s[MAXM][MAXM*10],ch[3],ss[MAXN];
  16. void add(char c,int x)
  17. {
  18. int l1=0,p=n;
  19. for(int i=1;i<=n;i++)
  20. {
  21. if(l1+len[i]>=x||i==n) {p=i;break;}
  22. l1+=len[i];
  23. }
  24. int pos=x-l1;len[p]=max(pos,len[p]+1);
  25. for(int i=len[p];i>pos;i--) s[p][i]=s[p][i-1];
  26. s[p][pos]=c;
  27. return;
  28. }
  29. char query(int x)
  30. {
  31. int l1=0,p=n;
  32. for(int i=1;i<=n;i++)
  33. {
  34. if(l1+len[i]>=x) {p=i;break;}
  35. l1+=len[i];
  36. }
  37. return s[p][x-l1];
  38. }
  39. void slove()
  40. {
  41. memset(len,0,sizeof len);
  42. memset(s,0,sizeof s);
  43. scanf("%d",&k);
  44. int l=strlen(ss),x=(l+999)/1000;
  45. n=(l-1)/x+1;
  46. for(int i=0;i<l;i++)
  47. s[i/x+1][i%x+1]=ss[i],len[i/x+1]++;
  48. while(k--)
  49. {
  50. scanf("%s",ch);
  51. if(ch[0]=='Q') scanf("%d",&x),printf("%c\n",query(x));
  52. else scanf("%s%d",ch,&x),add(ch[0],x);
  53. }
  54. }
  55. int main()
  56. {
  57. while(~scanf("%s",ss)) slove();
  58. }

Poj 2887 Big String(块状数组)的更多相关文章

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

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

  2. POJ 2887 Big String (块状数组)

    题意:给一个字符串(<=1000000)和n个操作(<2000),每个操作可以在某个位置插入一个字符,或者查询该位置的字符.问查询结果. 思路:块状数组. 如果将原来的字符串都存在一起,每 ...

  3. poj 2887 Big String

    题目连接 http://poj.org/problem?id=2887 Big String Description You are given a string and supposed to do ...

  4. Big String 块状数组(或者说平方分割)

    Big String 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 如果直接模拟的话, ...

  5. hdu 3553 Just a String (后缀数组)

    hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...

  6. codefroce D. Powerful array[初识块状数组]

    codefroce D. Powerful array[初识块状数组] 由于是初始所以,仅仅能先用别人的分析.囧... 题目: 给定一个数列:A1, A2,--,An,定义Ks为区间(l,r)中s出现 ...

  7. 范围运算符和索引的最终运算符 ^ 在string 和数组中的应用

    //范围运算符在string 和数组中的应用 static void Main(string[] args) { string examplestring = "123456789" ...

  8. POJ 2887:Big String(分块)

    http://poj.org/problem?id=2887 题意:给出一个字符串,还有n个询问,第一种询问是给出一个位置p和字符c,要在位置p的前面插入c(如果p超过字符串长度,自动插在最后),第二 ...

  9. Big String(poj 2887)

    题意: 给你一个不超过1e6的字符串,和不超过2000次的操作 操作分为两种: 1.将一个字符插入到某个位置的前面 2.询问当前位置的字符 /* 块状链表模板水题(我的智商也就能做这种题了). 观察题 ...

随机推荐

  1. 少儿编程Scratch第三讲:宇宙大战.枪战游戏

    小朋友这周的表现还算不错.周末多数时间都由我陪(bi)着(zhe)做课本上的数学题,后来还学了英语.任重道远啊,语数外都还得加强,还远不到自己就能取得好成绩的阶段. 上周说好这周要做一个发射炮弹的游戏 ...

  2. 监控提示message

    见文件 监控提示message.rar ---可作时时监控提示功能

  3. 移动端APP测试概要

    APP测试点总结(全面) 一.功能性测试: ——根据产品需求文档编写测试用例. ——软件设计文档编写用例. 注意:就是根据产品需求文档编写测试用例而进行测试. 二.兼容性测试: ——android版本 ...

  4. 怎样查看 MySQL 版本号

    1. 在命令行中直接查看版本号 mysql -V 2. 在 mysql --help 中查找与版本相关的信息 mysql --help | grep Ver 3. 在mysql命令行里面查看版本信息 ...

  5. JS基础_全局作用域

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. asp.net ListView控件的简单实用和配置

    1 web窗体界面代码 ItemType:控件要绑定的实体模型 SelectMethod:控件获取实体集合的后台方法 DataKeyNames:实体的主键 UpdateProduct:设置跟新的方法 ...

  7. 根据CPU核心数确定线程池并发线程数(转)

    一.抛出问题 关于如何计算并发线程数,一般分两派,来自两本书,且都是好书,到底哪个是对的?问题追踪后,整理如下: 第一派:<Java Concurrency in Practice>即&l ...

  8. django-bootstrap4|django 加载popper.min.js失败

    1.现象 2.解决过程 2.1.右键查看网页源代码 在浏览器地址栏打开popper.min.js对应的URL,发现无法打开,这个地址是国外的,需要找一个可访问的地址替换. 2.2.找到URL在djan ...

  9. mysql tinyint(1) 在java中被转化为boolean

    数据库表字段类型为:tinyint 长度为1 在java中对应的类型是boolean 查询时直接在页面展示成true或false 如果是2,3,4 这样的也是默认成true,非常不友好. 解决方案: ...

  10. linux入门常用指令4.挂载数据盘

    挂载硬盘 #查看当前分区情况 [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 5G 0 dis ...