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. 在win7中解决Visual C++ 6.0打开文件时出现停止工作问题

    在使用Visual C++ 6.0打开文件时可能会出现下面的情况 这可能是Vc6.0和win7兼容性问题. 方法: 下载filetool即可 链接:https://pan.baidu.com/s/1X ...

  2. selenium登录4399

    from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from seleni ...

  3. operator模块和functools模块

    operator模块 在函数式编程中,经常需要把算术运算符当作函数使用.例如,不使用 递归计算阶乘.求和可以使用 sum 函数,但是求积则没有这样的函数. 我们可以使用 reduce 函数(5.2.1 ...

  4. linux下mysql启动 Starting MySQL. ERROR! The server quit without updating PID file(xxx/x.pid)

    service mysql start 报错: Starting MySQL. ERROR! The server quit without updating PID file(xxx/x.pid) ...

  5. 用shell脚本安装MySQL-5.7.22-Percona版本

    #!/bin/bash MySQL_Package=Percona-Server-5.7.22-22-Linux.x86_64.ssl101.tar.gz Package_Source=Percona ...

  6. js将阿拉伯数字转换成汉字大写

    适用场景:票据,结算凭证等.将任意数字的金额,转换成汉字大写的形式.例如:1234.50 -> 壹仟贰佰叁拾肆圆伍角.壹.贰.叁.肆 直接贴代码,如下: //阿拉伯数字转换成大写汉字 funct ...

  7. SvcUtil.exe工具生成客户端代理类

    1.以管理员身份运行vs下命令工具: 2.运行代码示例:C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin>svcutil http: ...

  8. [转载]linux的top命令中cpu信息的含义

    https://www.cnblogs.com/wjoyxt/p/4918742.html 原文很好,我就不摘录了.

  9. Windows 下 nvm, node, npm 的下载、安装与配置

    主要解决的问题 下载安装完 nvm 和 node 后,缺失 npm 文件 执行 jasmine 等命令时提示「不是内部或外部命令...」及全局变量的设置 下载与安装 一.nvm github 下载地址 ...

  10. vue中非父子组件的传值bus的使用

    非父子之间的组件传值,可以使用vuex.简单的状态管理,也可以用vue bus vue bus可以实现不同组件间.不同页面间的通信,比如我在A页面出发点击事件,要B页面发生变化,使用方法如下: 全局定 ...