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]) 否则输 ...
随机推荐
- 大陆地区OpenStack项目Core现状(截至2016年1月28日,转载自陈沙克日志)
陈沙克 经常有朋友问,大陆地区大概有多少位OpenStack项目的Core.这个问题,现在其实不太好回答,如果需要准确统计的话.下面仅仅是一个大概估计,有遗漏的,希望朋友指出,我来补全. 文档修改历史 ...
- ORA-12518,TNS:listener could not hand off client connection
前几天在启动应用的时候,在控制台抛出了此异常信息!很明显是数据库方面的问题,不过具体是什么问题哪?百度了一下,网上关于此问题的信息还是有比较多,从异常的提示中我们也能看到是具体是和客户端的连接相关的问 ...
- 如何利用jq来实现复选框的全选,反选!
$("document").ready(function(){ $("#btn1").click(function(){ $("[name='chec ...
- SQL更新表的字段
Oracle: alter table CAPIAGENTLOG modify(clientcode nvarchar2()) SQL Server: alter table CAPIAGENTLOG ...
- ubuntu 12.04 64位设置兼容32位的实现
在ubuntu12.04上,要运行32的程序,需要安装32位的兼容库. 以前在10.04上成功安装过,方法是 sudo apt-get install ia32-libs 但是在12.04上遇到了困难 ...
- 标签栏控制器(UITabBarController)
一.标签栏控制器基础 1.绪论 a.一个标签栏是一个独立的界面对象,它经常与一个视图控制器(UITabBarController是UIViewController的子类)一起使用. b.一个标签栏项目 ...
- 【转】java 访问.net webservice返回的数据集
转自[转的也是转的][http://blog.csdn.net/fox123871/article/details/8637839] 1. 概述 很多正在开发或者打算开发XML Web Service ...
- 前端基础 - Defer对象
参考:http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html < ...
- C#语法基础和面向对象编程
1.C#语法基础 http://www.cnblogs.com/tonney/archive/2011/03/16/1986456.html 2.C#与面向对象基础 很棒的资源,简明扼要,介绍的非常清 ...
- 使用nodejs引用socket.io做聊天室
Server: var app = require('http').createServer(handler) , io = require('socket.io').listen(app) , fs ...