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.询问当前位置的字符 /* 块状链表模板水题(我的智商也就能做这种题了). 观察题 ...
随机推荐
- Android Studio中怎么使用DDMS工具
随着android studio的广泛使用,开发人员对相关工具的使用需求更加凸显.昨天在一个android studio教程网站上,看到一篇有关DDMS工具使用的相关知识,感觉很不错,分享给大家,一起 ...
- JWT与Session比较和作用
1. JSON Web Token是什么 JSON Web Token (JWT)是一个开放标准(RFC 7519),它定义了一种紧凑的.自包含的方式,用于作为JSON对象在各方之间安全地传输信息.该 ...
- idea中创建的go项目,添加project sdk时没有go sdk选项的解决方式
同样是后端开发,年薪50万和年薪20万的差距在哪里>>> 更新: 为了防止你被我这个流水账气到,先看这个结论吧:这个问题的结局方法:忽略,没有什么影响. -------------- ...
- linux - 卸载python
2019年10月15日12:05:42 [root@spider1 bin]# rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ##强制 ...
- gogs 搭建
sudo apt-get install nginx sudo apt-get install git sudo apt-get install mysql-server mysql -u root ...
- js面向对象的几种方式
对象的字面量 var obj={}:创建实例对象 var obj=new Object();构造函数模式 function fn(){}, new fn();工厂模式:用一个函数,通过传递参数返回对象 ...
- 网页免费转换为可编辑的PDF
Chrome自带的"打印"功能中,另存为PDF 可选择保存选中的内容.如果浏览器/网络出错,不能纠正.(推荐0) https://www.printfriendly.com (有C ...
- ASE19团队项目alpha阶段model组 scrum10 记录
本次会议于11月14日,19时整在微软北京西二号楼sky garden召开,持续5分钟. 与会人员:Jiyan He, Kun Yan, Lei Chai, Linfeng Qi, Xueqing W ...
- Spark集群任务提交流程----2.1.0源码解析
Spark的应用程序是通过spark-submit提交到Spark集群上运行的,那么spark-submit到底提交了什么,集群是怎样调度运行的,下面一一详解. 0. spark-submit提交任务 ...
- Django的ORM获取单表数据的三种方法
前言主题是从数据库取数据,把数据展现到前端客户端 一共有三种方法如下: 1,以对象的方法: 2,以字典的方法: 3,以元组的方法: 以对象的方法 说明:获取的是QuerySet类型,输出的是每个元素都 ...