题目连接

http://poj.org/problem?id=2887

Big String

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:

  1. 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.
  2. 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

abcdf
7
Q 1
I g 2
I h 10
I k 1
Q 1
Q 3
Q 8

Sample Output

a
k
g
f

伸展树的插入,单点查询。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<set>
using std::set;
using std::sort;
using std::pair;
using std::swap;
using std::vector;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1 << 20;
char buf[N];
struct Node {
int s;
char dat;
Node *fa, *ch[2];
inline void set(int i, char j, Node *p) {
s = i, dat = j;
fa = ch[0] = ch[1] = p;
}
inline void push_up() {
s = ch[0]->s + ch[1]->s + 1;
}
inline bool dir() const {
return fa->ch[0] == this;
}
inline void link(Node *x, bool d) {
ch[d] = x;
x->fa = this;
}
};
struct SplayTree {
Node *root, *tail, *null, stack[N];
inline void init(int n) {
tail = &stack[0];
null = tail++;
null->set(0, '.', NULL);
root = newNode('.');
root->link(newNode('.'), 1);
Node *x = built(1, n);
root->ch[1]->link(x, 0);
root->ch[1]->push_up();
root->push_up();
splay(x, null);
}
inline Node *built(int l, int r) {
if(l > r) return null;
int mid = (l + r) >> 1;
Node *p = newNode(buf[mid - 1]);
p->ch[0] = built(l, mid -1);
if(p->ch[0] != null) p->ch[0]->fa = p;
p->ch[1] = built(mid + 1, r);
if(p->ch[1] != null) p->ch[1]->fa = p;
p->push_up();
return p;
}
inline Node *newNode(char dat) {
Node *p = tail++;
p->set(1, dat, null);
return p;
}
inline void rotate(Node *&x, bool d) {
Node *y = x->fa;
y->ch[!d] = x->ch[d];
x->fa = y->fa;
if(x->ch[d]->s) x->ch[d]->fa = y;
if(y->fa->s) y->fa->ch[!y->dir()] = x;
x->ch[d] = y;
y->fa = x, y->push_up();
if (y == root) root = x;
}
inline void splay(Node *x, Node *f) {
if(x == root) return;
while(x->fa != f) {
if(x->fa->fa == f) {
rotate(x, x->dir());
} else {
bool d = x->fa->dir();
if(d == x->dir()) rotate(x->fa, d), rotate(x, d);
else rotate(x, !d), rotate(x, d);
}
}
x->push_up();
}
inline Node *select(Node *x, int k) {
for(int t = 0; x->s; ) {
t = x->ch[0]->s;
if(t == k) break;
else if(k < t) x = x->ch[0];
else k -= t + 1, x = x->ch[1];
}
return x;
}
inline void insert(char dat, int k) {
if(k > root->s) k = root->s - 2;
k--;
splay(select(root, k), null);
splay(select(root, k + 1), root);
Node *x = newNode(dat);
root->ch[1]->link(x, 0);
root->ch[1]->push_up();
root->push_up();
splay(x, null);
}
inline char operator[](int k) {
splay(select(root, k), null);
return root->dat;
}
}spt;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int q, k;
char op, dat;
while(~scanf("%s", buf)) {
scanf("%d", &q);
spt.init(strlen(buf));
while(q--) {
getchar();
scanf("%c", &op);
if(op == 'Q') {
scanf(" %d", &k);
printf("%c\n", spt[k]);
} else {
scanf(" %c %d", &dat, &k);
spt.insert(dat, k);
}
}
}
return 0;
}

poj 2887 Big String的更多相关文章

  1. POJ 2887 Big String(块状链表)

    题目大意 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 做法分析 好多不同的做法都可以 ...

  2. Poj 2887 Big String(块状数组)

    Big String Time Limit: 1000MS Memory Limit: 131072K Description You are given a string and supposed ...

  3. POJ 2887 Big String (块状数组)

    题意:给一个字符串(<=1000000)和n个操作(<2000),每个操作可以在某个位置插入一个字符,或者查询该位置的字符.问查询结果. 思路:块状数组. 如果将原来的字符串都存在一起,每 ...

  4. POJ 2887:Big String(分块)

    http://poj.org/problem?id=2887 题意:给出一个字符串,还有n个询问,第一种询问是给出一个位置p和字符c,要在位置p的前面插入c(如果p超过字符串长度,自动插在最后),第二 ...

  5. Big String(poj 2887)

    题意: 给你一个不超过1e6的字符串,和不超过2000次的操作 操作分为两种: 1.将一个字符插入到某个位置的前面 2.询问当前位置的字符 /* 块状链表模板水题(我的智商也就能做这种题了). 观察题 ...

  6. POJ 2887

    #include <iostream> #include <string> #define MAXN 2000 using namespace std; struct node ...

  7. 2887 Big String

    splay瞎搞一下,正解是分块数组或分块链表,但是学不会啊! #include<cstdio> #include<cstdlib> #include<iostream&g ...

  8. 好题 线段树对数据的保存+离线的逆向插入 POJ 2887

    题目大意:给一个字符串,有插入和询问操作,每次往一个位置插入一个字符或者询问第p个位置的字符是什么. 思路:我们离线询问,逆向把所有的字符都插入给线段树,然后再查询就好了,每次都要记得插入线段树的最后 ...

  9. POJ 2406 Power String

    算出next数组. 对于任何一个循环字串,len-next[len]必为最小循环节长度 若len%(len-next[len])==0 即为循环字串,n=len/(len-next[len]) 否则输 ...

随机推荐

  1. 【练习】手工生成awr报告

    ①首先进入$ORACLE_HOME/rdbms/admin目录 [oracle@host02 ~]$ cd $ORACLE_HOME/rdbms/admin 该目录下 [oracle@host02 a ...

  2. 二模07day2解题报告

    T1.采药(medic) 有n个草药,要在m的时间内获得最大价值. 乍一看像是01背包,然而数据只能过50分. 考虑数据范围,t<=10,w<=10,所以只有121种草药.考虑多重背包的二 ...

  3. jetty简介

    Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开发人员可以将 ...

  4. css定义表格样式

    table.gridtable { font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width ...

  5. 关于hbase的read操作的深入研究 region到storefile过程

    这里面说的read既包括get,也包括scan,实际底层来看这两个操作也是一样的.我们将要讨论的是,当我们从一张表读取数据的时候hbase到底是怎么处理的.分二种情况来看,第一种就是表刚创建,所有pu ...

  6. 文件服务器迁移—FSMT

    www.canway.net/Lists/CanwayOriginalArticels/DispForm.aspx?ID=282

  7. c++库大全

    1.C++各大有名库的介绍——C++标准库 2.C++各大有名库的介绍——准标准库Boost 3.C++各大有名库的介绍——GUI 4.C++各大有名库的介绍——网络通信 5.C++各大有名库的介绍— ...

  8. java语言基础02

    一.Java语言基础(常量的概述和使用)(掌握) 1:什么是常量 就是在程序的执行过程中其值不发生改变的量. 2:Java中常量的分类 (1):字面值常量 (2):自定义常量(面向对象部分讲解) 3: ...

  9. dnw-linux的安装及使用

    <Tiny6410刷机指南>介绍了如何用USB线下载uboot,kernel,filesystem到开发板的nand flash,USB下载文件用到工具dnw.遗憾的是该教程提供的是win ...

  10. JavaScript 一些基础练习

    JavaScript为网页添加动态效果并实现与用户交互的功能.改变颜色以及宽高,隐藏或显示内容,取消设置 <style type="text/css"> body { ...