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

/*
块状数组裸题.
把链表和数组的特性结合起来.
各个块互不影响(包括长度).
定位的时候看在哪一块就可以了.
各种复杂度都是√n.
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 1000010
#define MAXM 1001
using namespace std;
int n,m,k,ans,tot,len[MAXN];
char s[MAXM][MAXM*10],ch[3],ss[MAXN];
void add(char c,int x)
{
int l1=0,p=n;
for(int i=1;i<=n;i++)
{
if(l1+len[i]>=x||i==n) {p=i;break;}
l1+=len[i];
}
int pos=x-l1;len[p]=max(pos,len[p]+1);
for(int i=len[p];i>pos;i--) s[p][i]=s[p][i-1];
s[p][pos]=c;
return;
}
char query(int x)
{
int l1=0,p=n;
for(int i=1;i<=n;i++)
{
if(l1+len[i]>=x) {p=i;break;}
l1+=len[i];
}
return s[p][x-l1];
}
void slove()
{
memset(len,0,sizeof len);
memset(s,0,sizeof s);
scanf("%d",&k);
int l=strlen(ss),x=(l+999)/1000;
n=(l-1)/x+1;
for(int i=0;i<l;i++)
s[i/x+1][i%x+1]=ss[i],len[i/x+1]++;
while(k--)
{
scanf("%s",ch);
if(ch[0]=='Q') scanf("%d",&x),printf("%c\n",query(x));
else scanf("%s%d",ch,&x),add(ch[0],x);
}
}
int main()
{
while(~scanf("%s",ss)) slove();
}

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. 树链剖分 树剖求lca 学习笔记

    树链剖分 顾名思义,就是把一课时分成若干条链,使得它可以用数据结构(例如线段树)来维护 一些定义: 重儿子:子树最大的儿子 轻儿子:除了重儿子以外的儿子 重边:父节点与重儿子组成的边 轻边:除重边以外 ...

  2. Redis 和 Memcached 各有什么优缺点,主要的应用场景是什么样的?

    1.显示最新的项目列表 2.删除与过滤 3.排行榜相关 4.按照用户投票和时间排序 5.处理过期项目 6.计数 7.特定时间内的特定项目 8.实时分析正在发生的情况,用于数据统计与防止垃圾邮件等 9. ...

  3. 【广搜】Keyboarding

    题目描述 给定一个r行c列的在电视上的“虚拟键盘”,通过“上,下,左,右,选择”共5个控制键,你可以移动电视屏幕上的光标来打印文本.一开始,光标在键盘的左上角,每次按方向键,光标总是跳到下一个在该方向 ...

  4. hdu 1506 最大子矩阵面积

    //写动态规划的题目 要把主要问题提炼出来 这里的问题就是求area=(j-k+1)*a[i]  如果找到j k是解决这个题目的关键 这里暴力求肯定是要超时的 这里用dp来优化 #include< ...

  5. 设计模式风格<一>;回调风格

    主程序,是一个人,有一个类是同事: static void Main(string[] args) { Console.WriteLine("Hello Go to Lunch?" ...

  6. 一次解决黑帽SEO的经历

    最近有个朋友跟我说他的网站被黑了,百度快照里显示的是另一个网站,如: 于是查找了些资料,终于找到了问题所在,记录如下: 关于黑帽SEO1.暗链:其实“暗链”就是看不见的网站链接,“暗链”在网站中的链接 ...

  7. kubernetes 集群内部访问外部的数据库endpoint

    k8s访问集群外独立的服务最好的方式是采用Endpoint方式,以mysql服务为例: 创建mysql-service.yaml apiVersion: v1 kind: Service metada ...

  8. springsecurity学习

    首先讲一下,没有用到数据库,然后觉得重要的就是security的配置securityConfig.class,不太会说(好像也不太会用),上图吧,也是学习狂神过来的 项目结构 大致效果 pom.xml ...

  9. linux文件目录详细介绍

    linux文件目录 目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里 /etc 存放系统管理和配置文件 /home 存放所有用户文件的根目录,是用户主目录的基点 ...

  10. js之数据类型(对象类型——构造器对象——日期)

    Date对象是js语言中内置的数据类型,用于提供日期与时间的相关操作.学习它之前我们先了解一下什么是GMT,什么时UTC等相关的知识. GMT: 格林尼治标准时间(Greenwich Mean Tim ...