poj 2887 Big String
题目连接
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:
- 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
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的更多相关文章
- POJ 2887 Big String(块状链表)
题目大意 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 做法分析 好多不同的做法都可以 ...
- Poj 2887 Big String(块状数组)
Big String Time Limit: 1000MS Memory Limit: 131072K Description You are given a string and supposed ...
- POJ 2887 Big String (块状数组)
题意:给一个字符串(<=1000000)和n个操作(<2000),每个操作可以在某个位置插入一个字符,或者查询该位置的字符.问查询结果. 思路:块状数组. 如果将原来的字符串都存在一起,每 ...
- POJ 2887:Big String(分块)
http://poj.org/problem?id=2887 题意:给出一个字符串,还有n个询问,第一种询问是给出一个位置p和字符c,要在位置p的前面插入c(如果p超过字符串长度,自动插在最后),第二 ...
- Big String(poj 2887)
题意: 给你一个不超过1e6的字符串,和不超过2000次的操作 操作分为两种: 1.将一个字符插入到某个位置的前面 2.询问当前位置的字符 /* 块状链表模板水题(我的智商也就能做这种题了). 观察题 ...
- POJ 2887
#include <iostream> #include <string> #define MAXN 2000 using namespace std; struct node ...
- 2887 Big String
splay瞎搞一下,正解是分块数组或分块链表,但是学不会啊! #include<cstdio> #include<cstdlib> #include<iostream&g ...
- 好题 线段树对数据的保存+离线的逆向插入 POJ 2887
题目大意:给一个字符串,有插入和询问操作,每次往一个位置插入一个字符或者询问第p个位置的字符是什么. 思路:我们离线询问,逆向把所有的字符都插入给线段树,然后再查询就好了,每次都要记得插入线段树的最后 ...
- POJ 2406 Power String
算出next数组. 对于任何一个循环字串,len-next[len]必为最小循环节长度 若len%(len-next[len])==0 即为循环字串,n=len/(len-next[len]) 否则输 ...
随机推荐
- PMP考试--关于职业道德
如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 ★四个价值标准(value standards) 责任(responsibility ...
- 检测zookeeper和kafka是否正常
cd $(dirname $) source ~/.bash_profile count_zoo=`ps -ef | grep "config/zookeeper.properties&qu ...
- noip2008 火柴棒等式
P1149 火柴棒等式 1.9K通过 3.7K提交 题目提供者该用户不存在 标签搜索/枚举模拟2008NOIp提高组 难度普及- 提交该题 讨论 题解 记录 题目描述 给你n根火柴棍,你可以拼出多 ...
- No.010 Regular Expression Matching
10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...
- mac下的一些常识
1,环境变量 EddydeMacBook-Pro:~ eddy$ vi ~/.bash_profile EddydeMacBook-Pro:~ eddy$ vim /etc/profile Eddyd ...
- css的border效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- c++ boost--------------boost::progress_timer小试。
//--------------------------------------------------------------------------- #include <vcl.h> ...
- C语言文法 改
<程序> -> <外部声明> | <程序> <外部声明> <外部声明> -> <函数定义> | &l ...
- css初涉
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android IOS WebRTC 音视频开发总结(六五)-- 给韩国电信巨头做咨询
本文主要总结咨询过程中的一些问题,文章最早发表在我们的微信公众号上,详见这里,欢迎关注微信公众号blackerteam 韩国电信巨头sk想了解国内移动互联网rtc现状,所以上周请我过去给他们的相关人员 ...