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 ...
随机推荐
- Spring-Context之二:使用Spring提供的测试框架进行测试
Spring框架是无侵入性的,所以你的代码可以完全是POJO(plain old java object),直接使用Junit就可以完成大部分的单元测试.但是在集成测试方面就比较吃力了.单元测试层面你 ...
- java web开发 图片上传功能
基本思路在于,配置路径,然后用java I/O的api将图片上传到该目录下. String photoPath = ServletActionContext.getServletContext( ...
- Android Studio 生成Release版,报Warning的解决办法
转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6242227.html 请尊重知识产权!!! 同步更新到CSDN:http://blog.csdn.net/ ...
- Spark安装指南
一.Windows环境安装Spark 1.安装Java环境:jdk-8u101-windows-x64 配置环境变量: (1)增加变量名:JAVA_HOME 变量值:C:\Program File ...
- QT on Android开发
1.安装QT 2.安装JDK 配置如下系统环境变量: JAVA_HOME D:\Java\jdk Path %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin CLASSPATH ...
- Linux系统中CPU使用率查询常用的5个命令
在程序开发中,我们一般都是在Linux系统上进行开发,因此对Linux系统的维护工作很重要.在Linux系统维护中,我们需要经常查看的就是cpu的使用率,分析系统的整体运行情况.那CPU使用率怎么查询 ...
- 【Python排序搜索基本算法】之深度优先搜索、广度优先搜索、拓扑排序、强联通&Kosaraju算法
Graph Search and Connectivity Generic Graph Search Goals 1. find everything findable 2. don't explor ...
- Spring 整合 Flex (BlazeDS)无法从as对象 到 Java对象转换的异常:org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.Date' to required type 'java.sql.Timestamp' for property 'wfsj'; nested exception is java.lang.Ill
异常信息如下: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value ...
- CGI综述
参考: 详说fastcgi,php-fpm的区别:http://segmentfault.com/q/1010000000256516 什么是CGI.FastCGI.PHP-CGI.PHP-FPM.S ...
- 更多文章请访问"程序旅途”
更多文章请访问我的个人独立博客 程序旅途