Poj 2887 Big String(块状数组)
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(块状数组)的更多相关文章
- POJ 2887 Big String(块状链表)
题目大意 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 做法分析 好多不同的做法都可以 ...
- POJ 2887 Big String (块状数组)
题意:给一个字符串(<=1000000)和n个操作(<2000),每个操作可以在某个位置插入一个字符,或者查询该位置的字符.问查询结果. 思路:块状数组. 如果将原来的字符串都存在一起,每 ...
- poj 2887 Big String
题目连接 http://poj.org/problem?id=2887 Big String Description You are given a string and supposed to do ...
- Big String 块状数组(或者说平方分割)
Big String 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 如果直接模拟的话, ...
- hdu 3553 Just a String (后缀数组)
hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...
- codefroce D. Powerful array[初识块状数组]
codefroce D. Powerful array[初识块状数组] 由于是初始所以,仅仅能先用别人的分析.囧... 题目: 给定一个数列:A1, A2,--,An,定义Ks为区间(l,r)中s出现 ...
- 范围运算符和索引的最终运算符 ^ 在string 和数组中的应用
//范围运算符在string 和数组中的应用 static void Main(string[] args) { string examplestring = "123456789" ...
- POJ 2887:Big String(分块)
http://poj.org/problem?id=2887 题意:给出一个字符串,还有n个询问,第一种询问是给出一个位置p和字符c,要在位置p的前面插入c(如果p超过字符串长度,自动插在最后),第二 ...
- Big String(poj 2887)
题意: 给你一个不超过1e6的字符串,和不超过2000次的操作 操作分为两种: 1.将一个字符插入到某个位置的前面 2.询问当前位置的字符 /* 块状链表模板水题(我的智商也就能做这种题了). 观察题 ...
随机推荐
- Centos7下永久修改mysql5.6最大连接数
由于解除系统限制,设置最大连接数时,量力而行.https://blog.csdn.net/five3/article/details/79671317
- npm install 报错踩坑路
先出现的是超过最大调用栈问题: npm ERR! Maximum call stack size exceeded 百度之后说给npm降级或者升级 降级 : npm install -g npm@5. ...
- 浅谈Promise原理与应用
在JavaScript中,所有代码都是单线程.由于该“缺陷”,JavaScript在处理网络操作.事件操作时都是需要进行异步执行的.AJAX就是一个典型的异步操作 对于异步操作,有传统的利用回调函数和 ...
- js中的函数提升和变量提升
变量提升和函数提升: 就是将变量声明或者函数全部代码提升到当前作用域(全局作用域或函数作用域)最开始的部分. JavaScript中函数域为最小域范围:for循环.while循环.if语句.switc ...
- 微信小程序跳到指定页面不生效
wx.redirectTo({ url: './index', }) wx.navigateTo({ url: './index', }) 用了这两种都没生效,点击没跳转,也没报错 后来想了下是不是因 ...
- Java BIO、NIO、AIO 基础,应用场景
Java对BIO.NIO.AIO的支持: Java BIO : 同步并阻塞,服务器实现模式为一个连接一个线程,即客户端有连接请求时服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不必 ...
- MySQL数据库常见问题1:关于 “ MySQL Installer is running in Community mode ” 的解决办法
现象: MYSQL在安装完成后,系统能正常运行,但是第二天出现了如下一个提示框,如下图: 给个人人都看得懂的如下图: 解决办法: 这个是新版本MySQL服务自带的一个定时任务,每天23: ...
- 在线预览word、excel文件
直接使用微软提供的在线预览服务. 免费 文件必须为网可访问地址,因为微软的服务器需要访问该文件
- IDEA中使用git合并分支的过程报错:cant checkout because of unmerged files
使用idea的git插件控制代码分支合并时,由于操作不当,报错了,控制台报错如下: cant checkout because of unmerged files,you have to resolv ...
- Tomcat - Tomcat安装
Tomcat官网:http://tomcat.apache.org/ 准备:JAVA环境布置完成 一.Windows平台 1. 版本选择 1) 进入官网 2) 查看版本匹配 官网说明 https:// ...