题目连接

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. redis(二)Redis适用场景,如何正确的使用

    网络IO模型 Memcached是多线程,非阻塞IO复用的网络模型,分为监听主线程和worker子线程,监听线程监听网络连接,接受请求后,将连接描述字pipe 传递给worker线程,进行读写IO, ...

  2. AD转换精度的计算

    声明原文来源于:http://wenku.baidu.com/view/1e6d8f3083c4bb4cf7ecd1c2.html 讨论AD转换分辨率的算法(zt) (1)在总长度为5米的范围里,平均 ...

  3. oracle 清除当前用户的回收站

    --清除当前用户的回收站:purge recyclebin;  --删除表数据truncate table --查看当前用户回收站select * from user_recyclebin t; 

  4. LA3211 飞机调度 Now or later-二分法&TwoSet

    https://vjudge.net/problem/UVALive-3211 As you must have experienced, instead of landing immediately ...

  5. Java基础类库

    1 main方法      运行java程序的参数:   下面详细讲解main 方法为什么采用这个方法签名 1.public 修饰符:Java类由jvm调用,为了让jvm可以自由调用这个main()方 ...

  6. MVC中使用过滤器记录异常日志

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Filte ...

  7. AX 最顶部工作区间窗口文本修改

    修改Class\Info\method\workspaceWindowCreated: void workspaceWindowCreated(int _hWnd) { // Put workspac ...

  8. 将windows系统装到USB存储设备

    需求: 1)一般公司比较规范,计算机系统有严格的限制策略,如果自己不懂得如何更改或者没有权限更改,将极其不便. 2)计划在家里完成在公司未完成的事,甚至异地出差觉得携带笔记本不太方便,寻找更便携的设备 ...

  9. THINKPHP 清除HTML注释、换行符、空格、制表符等

    thinkphp3.2 3.2中取消了配置文件中的 'TMPL_STRIP_SPACE' 属性,所以我们先来修改:\ThinkPHP\Library\Think\Template.class.php ...

  10. jQuery实现的Div窗口震动效果实例

    本文实例讲述了jQuery实现的Div窗口震动效果.分享给大家供大家参考.具体如下: 这是一款jQuery窗口震动效果代码,在Div边框内点击一下鼠标,它就开始震动了,适用浏览器:IE8.360.Fi ...