uva12538

本文介绍了一种使用可持久化Treap数据结构实现的文本编辑器,该编辑器支持插入和删除操作,并能自动保存版本,便于查看历史记录。文中详细解释了如何通过构建Treap来高效地处理这些操作,并附上了完整的代码实现。

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:

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<algorithm>
  6 using namespace std;
  7 char ch;
  8 bool ok;
  9 void read(int &x){
 10     ok=0;
 11     for (ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1;
 12     for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
 13     if (ok) x=-x;
 14 }
 15 typedef pair<int,int> pii;
 16 const int maxnode=4000000;
 17 const int maxn=50005;
 18 char s[maxnode];
 19 int q,op,tim,pos,id,len,cnt;
 20 int random(int lim){return rand()%lim+1;}
 21 struct treep{
 22     int root[maxn];
 23     char node[maxnode];
 24     int tot,son[maxnode][2],siz[maxnode];
 25     void init(){
 26         srand('f'+'u'+'c'+'k'),tot=0;
 27         memset(root,0,sizeof(root));
 28         memset(son,0,sizeof(son));
 29         memset(siz,0,sizeof(siz));
 30     }
 31     void update(int a){
 32         siz[a]=1;
 33         if (son[a][0]) siz[a]+=siz[son[a][0]];
 34         if (son[a][1]) siz[a]+=siz[son[a][1]];    
 35     }
 36     int newnode(char ch,int ls,int rs){
 37         node[++tot]=ch,son[tot][0]=ls,son[tot][1]=rs,update(tot);
 38         return tot;
 39     }
 40     int build(int l,int r){
 41         if (l>r) return 0;
 42         int m=(l+r)>>1;
 43         return newnode(s[m],build(l,m-1),build(m+1,r));
 44     }
 45     pii split(int a,int k){
 46         if (!k) return make_pair(0,a);
 47         if (k==siz[a]) return make_pair(a,0);
 48         pii tmp;
 49         if (k<=siz[son[a][0]]){
 50             tmp=split(son[a][0],k);
 51             return make_pair(tmp.first,newnode(node[a],tmp.second,son[a][1]));
 52         }
 53         else{
 54             tmp=split(son[a][1],k-siz[son[a][0]]-1);
 55             return make_pair(newnode(node[a],son[a][0],tmp.first),tmp.second);
 56         }
 57     }
 58     int merge(int a,int b){
 59         if (!a||!b) return a+b;
 60         if (random(siz[a]+siz[b])<=siz[a]) return newnode(node[a],son[a][0],merge(son[a][1],b));
 61         else return newnode(node[b],merge(a,son[b][0]),son[b][1]);
 62     }
 63     void watch(int a){
 64         if (son[a][0]) watch(son[a][0]);
 65         putchar(node[a]);
 66         if (node[a]=='c') cnt++;
 67         if (son[a][1]) watch(son[a][1]);    
 68     }
 69     void insert(int id,int pos){
 70         int a,b,c;
 71         pii tmp=split(root[id],pos);
 72         a=tmp.first,c=tmp.second,b=build(1,len);
 73         root[++tim]=merge(merge(a,b),c);
 74     }
 75     void remove(int id,int l,int r){
 76         int a,b,c;
 77         pii tmp=split(root[id],r);
 78         c=tmp.second,tmp=split(tmp.first,l-1),a=tmp.first,b=tmp.second;
 79         root[++tim]=merge(a,c);    
 80     }
 81     void look(int id,int l,int r){
 82         int a,b,c;
 83         pii tmp=split(root[id],r);
 84         c=tmp.second,tmp=split(tmp.first,l-1),a=tmp.first,b=tmp.second;
 85         watch(b),puts("");
 86     }
 87 }T;
 88 int main(){
 89     for (read(q);q;q--){
 90         read(op);
 91         if (op==1){
 92             read(pos),scanf("%s",s+1),len=strlen(s+1),pos-=cnt;
 93             T.insert(tim,pos);
 94         }
 95         else if (op==2){
 96             read(pos),read(len),pos-=cnt,len-=cnt;
 97             T.remove(tim,pos,pos+len-1);
 98         }
 99         else if (op==3){
100             read(id),read(pos),read(len),id-=cnt,pos-=cnt,len-=cnt;
101             T.look(id,pos,pos+len-1);
102         }
103     }
104     return 0;
105 }

 

转载于:https://www.cnblogs.com/chenyushuo/p/5183341.html

根据原作 https://pan.quark.cn/s/459657bcfd45 的源码改编 Classic-ML-Methods-Algo 引言 建立这个项目,是为了梳理和总结传统机器学习(Machine Learning)方法(methods)或者算法(algo),和各位同仁相互学习交流. 现在的深度学习本质上来自于传统的神经网络模型,很大程度上是传统机器学习的延续,同时也在不少时候需要结合传统方法来实现. 任何机器学习方法基本的流程结构都是通用的;使用的评价方法也基本通用;使用的一些数学知识也是通用的. 本文在梳理传统机器学习方法算法的同时也会顺便补充这些流程,数学上的知识以供参考. 机器学习 机器学习是人工智能(Artificial Intelligence)的一个分支,也是实现人工智能最重要的手段.区别于传统的基于规则(rule-based)的算法,机器学习可以从数据中获取知识,从而实现规定的任务[Ian Goodfellow and Yoshua Bengio and Aaron Courville的Deep Learning].这些知识可以分为四种: 总结(summarization) 预测(prediction) 估计(estimation) 假想验证(hypothesis testing) 机器学习主要关心的是预测[Varian在Big Data : New Tricks for Econometrics],预测的可以是连续性的输出变量,分类,聚类或者物品之间的有趣关联. 机器学习分类 根据数据配置(setting,是否有标签,可以是连续的也可以是离散的)和任务目标,我们可以将机器学习方法分为四种: 无监督(unsupervised) 训练数据没有给定...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值