uva12538
12538 Version Controlled IDE
Programmers use version control systems to manage files in their projects, but in these systems, versions
are saved only when you manually submit. Can you implement an IDE that automatically saves a new
version whenever you insert or delete a string?
Positions in the buffer are numbered from 1 from left to right. Initially, the buffer is empty and in
version 0. Then you can execute 3 commands (vnow means the version before executing the command,
and L[v] means the length of buffer at version v):
• 1 p s: insert string s after position p (0 ≤ p ≤ L[vnow], p = 0 means insert before the start of the
buffer). s contains at most 1 and at most 100 letters.
• 2 p c: remove c characters starting at position p (p ≥ 1, p + c ≤ L[vnow] + 1). The remaining
charactesr (if any) will be shifted left, filling the blank
• 3 v p c: print c characters starting at position p (p ≥ 1, p + c ≤ L[v] + 1), in version v (1 ≤ v ≤
vnow).
The first command is guaranteed to be command 1(insert). After executing each command 1 or 2,
version is incremented by 1.
Input
There is only one test case. It begins with a single integer n (1 ≤ n ≤ 50, 000), the number of commands.
Each of the following n lines contains a command. The total length of all inserted string will not
exceed 1,000,000.
Output
Print the results of command 3, in order. The total length of all printed strings will not exceed 200,000.
Obfuscation:
In order to prevent you from preprocessing the command, we adopt the following obfuscation scheme:
• Each type-1 command becomes 1 p + d s
• Each type-2 command becomes 2 p + d c + d
• Each type-3 command becomes 3 v + d p + d c + d
Where d is the number of lowercase letter ‘c’ you printed, before processing this command.
Before the obfuscation, the sample input would be:
6
1 0 abcdefgh
2 4 3
3 1 2 5
3 2 2 3
1 2 xy
3 3 2 4
This is the real input that your program must process when it reads the Sample Input
below.
Sample Input
6
1 0 abcdefgh
2 4 3
3 1 2 5
3 3 3 4
1 4 xy
3 5 4 6
Sample Output
bcdef
bcg
bxyc
题解:题意就是要你实现一个超级文本编辑器(只有插入和删除),然后这就是可持久化treep了
code:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
char ch;
bool ok;
void read(int &x){
ok=;
for (ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
typedef pair<int,int> pii;
const int maxnode=;
const int maxn=;
char s[maxnode];
int q,op,tim,pos,id,len,cnt;
int random(int lim){return rand()%lim+;}
struct treep{
int root[maxn];
char node[maxnode];
int tot,son[maxnode][],siz[maxnode];
void init(){
srand('f'+'u'+'c'+'k'),tot=;
memset(root,,sizeof(root));
memset(son,,sizeof(son));
memset(siz,,sizeof(siz));
}
void update(int a){
siz[a]=;
if (son[a][]) siz[a]+=siz[son[a][]];
if (son[a][]) siz[a]+=siz[son[a][]];
}
int newnode(char ch,int ls,int rs){
node[++tot]=ch,son[tot][]=ls,son[tot][]=rs,update(tot);
return tot;
}
int build(int l,int r){
if (l>r) return ;
int m=(l+r)>>;
return newnode(s[m],build(l,m-),build(m+,r));
}
pii split(int a,int k){
if (!k) return make_pair(,a);
if (k==siz[a]) return make_pair(a,);
pii tmp;
if (k<=siz[son[a][]]){
tmp=split(son[a][],k);
return make_pair(tmp.first,newnode(node[a],tmp.second,son[a][]));
}
else{
tmp=split(son[a][],k-siz[son[a][]]-);
return make_pair(newnode(node[a],son[a][],tmp.first),tmp.second);
}
}
int merge(int a,int b){
if (!a||!b) return a+b;
if (random(siz[a]+siz[b])<=siz[a]) return newnode(node[a],son[a][],merge(son[a][],b));
else return newnode(node[b],merge(a,son[b][]),son[b][]);
}
void watch(int a){
if (son[a][]) watch(son[a][]);
putchar(node[a]);
if (node[a]=='c') cnt++;
if (son[a][]) watch(son[a][]);
}
void insert(int id,int pos){
int a,b,c;
pii tmp=split(root[id],pos);
a=tmp.first,c=tmp.second,b=build(,len);
root[++tim]=merge(merge(a,b),c);
}
void remove(int id,int l,int r){
int a,b,c;
pii tmp=split(root[id],r);
c=tmp.second,tmp=split(tmp.first,l-),a=tmp.first,b=tmp.second;
root[++tim]=merge(a,c);
}
void look(int id,int l,int r){
int a,b,c;
pii tmp=split(root[id],r);
c=tmp.second,tmp=split(tmp.first,l-),a=tmp.first,b=tmp.second;
watch(b),puts("");
}
}T;
int main(){
for (read(q);q;q--){
read(op);
if (op==){
read(pos),scanf("%s",s+),len=strlen(s+),pos-=cnt;
T.insert(tim,pos);
}
else if (op==){
read(pos),read(len),pos-=cnt,len-=cnt;
T.remove(tim,pos,pos+len-);
}
else if (op==){
read(id),read(pos),read(len),id-=cnt,pos-=cnt,len-=cnt;
T.look(id,pos,pos+len-);
}
}
return ;
}
uva12538的更多相关文章
- UVA12538 Version Controlled IDE
题意翻译 维护一种数据结构,资磁三种操作. 1.在p位置插入一个字符串s 2.从p位置开始删除长度为c的字符串 3.输出第v个历史版本中从p位置开始的长度为c的字符串 1≤n≤50000,所有字符串总 ...
随机推荐
- Cookie及App登陆的原理
1.Cookie Cookie意为"甜饼",是由W3C组织提出的.目前Cookie已经成为标准.由于HTTP是一种无状态的协议,服务器单从网络连接上无从知道客户身份.怎么办呢?就给 ...
- java实现url转码、解码
URL由来: 一般来说,URL只能使用英文字母.阿拉伯数字和某些标点符号,不能使用其他文字和符号.比如,世界上有英文字母的网址 “http://www.abc.com”,但是没有希腊字母的网址“htt ...
- ArrayList and LinkedList
ArrayList and LinkedList List代表一种线性表的数据结构,ArrayList则是一种顺序存储的线性表.ArrayList底层采用数组来保存每个集合元素,LinkedList则 ...
- Delphi 记事本 TMemo
Windows记事本记事本 描述: 用Delphi模仿的Windows记事本 界面和功能都和Windows的记事本一样,是用Memo实现的而不是RichEdit 可以执行以下功能 文件 ...
- TopCoder SRMS 1 字符串处理问题 Java题解
Problem Statement Let's say you have a binary string such as the following: 011100011 One way to e ...
- Windows如何打包Qt程序
很多Qt爱好者想发布自己的Qt软件,但却发现在其他没有安装Qt SDK的机器上无法运行,这就是本文想要说明的问题.现在网上大部分软件都要发布自己开发的应用程序,都会打包到exe文件中,待安装完exe文 ...
- Python建立socket并获取信息
import socket, sys port = 80 host = "www.baidu.com" print "Creating socket..." s ...
- Linux shell用法和技巧
使用Linux shell是我每天的基本工作,但我经常会忘记一些有用的shell命令和l技巧.当然,命令我能记住,但我不敢说能记得如何用它执行某个特定任务.于是,我开始在一个文本文件里记录这 ...
- oracle中的层级递归查询操作
oracle中的层级操作非常方便,在使用之后爱不释手,以前要实现该种数据查询操作,需要非常复杂的实现过程.在oracle中通过connect by可以实现前面的目的,通常情况下层级查询基本都能实现递归 ...
- iOS socket编程
// // ViewController.m // socket // // Created by emerys on 16/3/2. // Copyright © 2016年 Emerys. All ...