POJ 2887 Big String(块状链表)
题目大意
给一个字符串,长度不超过 106,有两种操作:
1. 在第 i 个字符的前面添加一个字符 ch
2. 查询第 k 个位置是什么字符
操作的总数不超过 2000
做法分析
好多不同的做法都可以搞
人生第一个块状链表,记录下
块状链表的思想其实挺简单的,传统的链表每个节点只记录一个字符,块状链表的每个节点记录的是 sqrt(n) 个信息,一个长度为 n 的字符串就被分成了 sqrt(n) 个。这样,查询和插入字符的操作就变成了 sqrt(n)级别的了,对于这题 2000 个操作来讲,时间复杂度还能忍受
这题只有插入和查询操作,所以写起来非常简单
不过我不喜欢使用指针搞来稿去的,所以写的数组形式的
参考代码
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; const int N=, LEN=; struct Block_List {
struct Node {
char buff[LEN];
int next, Size;
void init() {
memset(buff, , sizeof buff);
next=-, Size=;
}
} List[N];
int head, tot; void init(char S[]) {
head=tot=;
List[tot++].init();
for(int i=, cur=head; S[i]; cur=List[cur].next) {
for(int j=; j<LEN && S[i]; j++, i++) {
List[cur].buff[j]=S[i];
List[cur].Size++;
}
if(S[i]) {
List[tot].init();
List[cur].next=tot++;
}
}
for(int cur=head; cur!=-; cur=List[cur].next)
if(List[cur].Size==LEN) Split(cur);
} void Split(int id) {
List[tot].init();
for(int i=LEN/; i<LEN; i++) {
List[tot].buff[i-LEN/]=List[id].buff[i];
List[tot].Size++;
List[id].buff[i]=;
List[id].Size--;
}
List[tot].next=List[id].next;
List[id].next=tot++;
} void Insert(int pos, char val) {
int cur=head;
while(pos>List[cur].Size && List[cur].next!=-) {
pos-=List[cur].Size;
cur=List[cur].next;
}
if(pos>=List[cur].Size) List[cur].buff[List[cur].Size]=val;
else {
for(int i=List[cur].Size; i>pos; i--) List[cur].buff[i]=List[cur].buff[i-];
List[cur].buff[pos]=val;
}
List[cur].Size++;
if(List[cur].Size==LEN) Split(cur);
} char Find(int pos) {
int cur=head;
while(pos>List[cur].Size) pos-=List[cur].Size, cur=List[cur].next;
return List[cur].buff[pos-];
}
}; Block_List hehe;
char buff[], S[];
int n; int main() {
// freopen("in", "r", stdin);
scanf("%s", buff);
hehe.init(buff);
scanf("%d", &n);
for(int i=, pos; i<n; i++) {
scanf("%s", buff);
if(buff[]=='I') {
scanf("%s%d", S, &pos);
hehe.Insert(pos-, S[]);
}
else {
scanf("%d", &pos);
printf("%c\n", hehe.Find(pos));
}
}
return ;
}
POJ 2887
题目链接 & AC 通道
POJ 2887 Big String(块状链表)的更多相关文章
- Poj 2887 Big String(块状数组)
Big String Time Limit: 1000MS Memory Limit: 131072K Description You are given a string and supposed ...
- poj 2887 Big String
题目连接 http://poj.org/problem?id=2887 Big String Description You are given a string and supposed to do ...
- POJ 2887 Big String (块状数组)
题意:给一个字符串(<=1000000)和n个操作(<2000),每个操作可以在某个位置插入一个字符,或者查询该位置的字符.问查询结果. 思路:块状数组. 如果将原来的字符串都存在一起,每 ...
- 【POJ2887】【块状链表】Big String
Description You are given a string and supposed to do some string manipulations. Input The first lin ...
- Big String(poj 2887)
题意: 给你一个不超过1e6的字符串,和不超过2000次的操作 操作分为两种: 1.将一个字符插入到某个位置的前面 2.询问当前位置的字符 /* 块状链表模板水题(我的智商也就能做这种题了). 观察题 ...
- 【BZOJ1500】【块状链表】维修数列
Description Input 输入文件的第1行包含两个数N和M,N表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一条命令,格式参见问题描述 ...
- 【BZOJ2741】【块状链表+可持久化trie】FOTILE模拟赛L
Description FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max(Ai xor Ai+1 xor Ai+2 .. ...
- 【BZOJ3295】【块状链表+树状数组】动态逆序对
Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...
- 【HDU4391】【块状链表】Paint The Wall
Problem Description As a amateur artist, Xenocide loves painting the wall. The wall can be considere ...
随机推荐
- AngularJS应用页面切换优化方案
葡萄城的一款尚在研发中的产品,对外名称暂定为X项目.其中使用了已经上市的Wijmo中SpreadJS产品,另外,在研发过程中整理了一些研发总结分享给大家.如本篇的在页面切换的过程中优化方案,欢迎大家跟 ...
- ActiveReports 9实战教程(3): 图文并茂的报表形式
基于上面2节内容,我们搭建了AR9的开发环境,配置好了数据源.在本节,我们以官方提供的3个中文图文并茂的报表来展示AR9的功能,并通过实战的方式一一分享. 以往做报表相关的工作时,最害怕的是报表的UI ...
- [RabbitMQ] AMQP close-reason, initiated by Library, code=541
RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachabl ...
- 浅谈sql中的in与not in,exists与not exists的区别
转 浅谈sql中的in与not in,exists与not exists的区别 12月12日北京OSC源创会 —— 开源技术的年终盛典 » sql exists in 1.in和exists ...
- paip.不同目录结构哈的文件批量比较
paip.不同目录结构哈的文件批量比较 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...
- 重新签名IPA ( iPhone )
提示:暂时不能用了,企业证书滥用 ios 企业证书 ipa 重新签名发布 1. 应用场景 当前有一个 未用企业证书签名的 ipa 文件,默认是不可以直接安装到设备上的:我们需要用企业版证书签名: 当前 ...
- Leetcode 231 Power of Two 数论
同样是判断数是否是2的n次幂,同 Power of three class Solution { public: bool isPowerOfTwo(int n) { ) && ((( ...
- React学习资料
以下是我整理的React学习资料,包括:React基础.Redux.reat-router, redux middleware, higher order components, React验证等, ...
- zz c++ Useful resources
Useful resources < cpp The Standard C++ Foundation - Non-profit hub for C++ news, articles, and e ...
- 求n*m网格内矩形的数目
一个n*m的网格,求这个网格中矩形的数目. 比如以下2*2网格,总共有9个矩形:4个1*1的矩形,4个1*2的矩形,1个2*2的矩形 算法1:动态规划,假设dp[i][j]表示以第 i 行第 j ...