题目:http://poj.org/problem?id=2887
Big String
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 6527   Accepted: 1563

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

ab
7
Q 1
I c 2
I d 4
I e 2
Q 5
I f 1
Q 3

Sample Output

a
d
e

Source

POJ Monthly--2006.07.30, zhucheng

题意:给一个字符串,要求查询某一位的字母,或在某一位前插入一个字母.

题解:

Splay的单点插入,和单点查询.

记得内存大小要把操作的2000加上。

速度422ms,还不错。。。

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cmath>
  5 #include<iostream>
  6 #include<algorithm>
  7 using namespace std;
  8 #define MAXN 1002010
  9 struct node
 10 {
 11     int left,right,size;
 12     char val;
 13 }tree[MAXN];
 14 int father[MAXN];
 15 char str[MAXN];
 16 void Pushup(int k)
 17 {
 18     tree[k].size=tree[tree[k].left].size+tree[tree[k].right].size+1;
 19 }
 20 void rotate(int x,int &root)
 21 {
 22     int y=father[x],z=father[y];
 23     if(y==root)root=x;
 24     else
 25     {
 26         if(tree[z].left==y)tree[z].left=x;
 27         else tree[z].right=x;
 28     }
 29     if(tree[y].left==x)
 30     {
 31         father[x]=z;father[y]=x;tree[y].left=tree[x].right;tree[x].right=y;father[tree[y].left]=y;
 32     }
 33     else
 34     {
 35         father[x]=z;father[y]=x;tree[y].right=tree[x].left;tree[x].left=y;father[tree[y].right]=y;
 36     }
 37     Pushup(y);Pushup(x);
 38 }
 39 void Splay(int x,int &root)
 40 {
 41     while(x!=root)
 42     {
 43         int y=father[x],z=father[y];
 44         if(y!=root)
 45         {
 46             if((tree[y].left==x)^(tree[z].left==y))rotate(x,root);
 47             else rotate(y,root);
 48         }
 49         rotate(x,root);
 50     }
 51 }
 52 void Build(int l,int r,int f)
 53 {
 54     if(l>r)return;
 55     int now=l,fa=f;
 56     if(l==r)
 57     {
 58         tree[now].val=str[l];tree[now].size=1;
 59         father[now]=fa;
 60         if(l<f)tree[fa].left=now;
 61         else tree[fa].right=now;
 62         return;
 63     }
 64     int mid=(l+r)/2;
 65     now=mid;
 66     Build(l,mid-1,mid);Build(mid+1,r,mid);
 67     tree[now].val=str[mid];father[now]=fa;
 68     Pushup(now);
 69     if(mid<f)tree[fa].left=now;
 70     else tree[fa].right=now;
 71 }
 72 int Find(int root,int rank)
 73 {
 74     if(rank==tree[tree[root].left].size+1)return root;
 75     if(rank<=tree[tree[root].left].size)return Find(tree[root].left,rank);
 76     else return Find(tree[root].right,rank-tree[tree[root].left].size-1);
 77 }
 78 int main()
 79 {
 80     int m,i,k,L,R,x,y,z,lstr,rt,n;
 81     char ch,fh[2];
 82     scanf("%s",str+2);
 83     lstr=strlen(str+2);
 84     m=lstr+2;
 85     Build(1,m,0);
 86     rt=(1+m)/2;
 87     scanf("%d",&n);
 88     for(i=1;i<=n;i++)
 89     {
 90         scanf("\n%s",fh);
 91         if(fh[0]=='Q')
 92         {
 93             scanf("%d",&k);
 94             L=k;R=k+2;
 95             x=Find(rt,L);y=Find(rt,R);
 96             Splay(x,rt);Splay(y,tree[x].right);
 97             z=tree[y].left;
 98             printf("%c\n",tree[z].val);
 99         }
100         else
101         {
102             scanf("\n%c %d",&ch,&k);
103             L=k;R=k+1;
104             x=Find(rt,L);y=Find(rt,R);
105             Splay(x,rt);Splay(y,tree[x].right);
106             tree[y].left=++m;
107             z=tree[y].left;tree[z].size=1;
108             father[z]=y;tree[z].val=ch;
109             Pushup(y);Pushup(x);
110         }
111     }
112     return 0;
113 }

View Code

转载于:https://www.cnblogs.com/Var123/p/5275769.html

Poj 2887-Big String Splay相关推荐

  1. POJ 2887 Big String

    Big String Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7053 Accepted: 1684 Descripti ...

  2. poj 2892 Tunnel Warfare (Splay Tree instead of Segment Tree)

    poj.org/problem?id=2892 poj上的一道数据结构题,这题正解貌似是Segment Tree,不过我用了Splay Tree来写,而且我个人认为,这题用Splay Tree会更好写 ...

  3. oracle rman备份时间点,Oracle Rman 控制RMAN的备份时间,减少IO消耗

    一.问题描述 由于服务器配置不高,备份策略为周末全备.周一至周六差异备份. 平时服务器CPU使用30%左右. 全备份时,开启两个通道,CPU达到70%-80%左右,业务不卡顿.不掉单,session不 ...

  4. POJ 1002 487-3279

    2019独角兽企业重金招聘Python工程师标准>>> 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...

  5. 平衡树初阶——AVL平衡二叉查找树+三大平衡树(Treap + Splay + SBT)模板【超详解】...

    平衡树初阶--AVL平衡二叉查找树 一.什么是二叉树 1. 什么是树. 计算机科学里面的树本质是一个树状图.树首先是一个有向无环图,由根节点指向子结点.但是不严格的说,我们也研究无向树.所谓无向树就是 ...

  6. 字符串匹配のKMP【专题@AbandonZHANG】

    算法详解 很长时间内都没有能够很理解KMP算法的精髓,尤其是很多书上包括<算法导论>没有把next函数(亦或 π函数)讲解的很透彻. 今天去看了matrix67大牛博客中关于kmp部分的讲 ...

  7. 字符串匹配のKMP【@Abandon】

    算法详解 很长时间内都没有能够很理解KMP算法的精髓,尤其是很多书上包括<算法导论>没有把next函数(亦或 π函数)讲解的很透彻. 今天去看了matrix67大牛博客中关于kmp部分的讲 ...

  8. Java知识——精华总结

    Java知识--精华总结 一.java概述与基础知识 1.何为编程? 编程就是让计算机为解决某个问题而使用某种程序设计语言编写程序代码,并最终得到结果的过程. 为了使计算机能够理解人的意图,人类就必须 ...

  9. 【POJ】2828 Buy Tickets(线段树+特殊的技巧/splay)

    http://poj.org/problem?id=2828 一开始敲了个splay,直接模拟. tle了.. 常数太大.. 好吧,说是用线段树.. 而且思想很拽.. (貌似很久以前写过貌似的,,) ...

最新文章

  1. Glide二次封装库的使用
  2. 随机森林中的Bootstrap抽样是有放回抽样么?Bootstrap抽样过程描述一下
  3. [py2neo]Ubuntu14 安装py2neo失败问题解决
  4. [蓝桥杯]算法提高 vertex cover(dfs)
  5. iPhone屏幕各种尺寸分辨率(更新至XS)
  6. [react] react的函数式组件有没有生命周期?
  7. Spark在美团的实践
  8. java get post 注解,GET/POST接收或发送数据的问题
  9. python读取html文件中的表格数据_使用解析html表pd.read_html文件其中单元格本身包含完整表...
  10. 深度学习——简化的神经网络模型和概念补充
  11. 3d模型多怎么优化_3D打印人像模型是怎么制作出来的呢?
  12. 中移4G模块-ML302-OpenCpu开发-(MQTT连接阿里云-RRPC通讯)
  13. win11 c4d如何安装 Windows11安装c4d的步骤方法
  14. C语言:求矩阵非对角线元素的和
  15. Android--建立能与访问者进行相互通信的本地服务
  16. Sakai 20.0 部署
  17. 关于计算机作文的结束语,语文老师:万能作文开头30篇结尾50篇,吃透次次“满分范文”...
  18. 微信小程序个人/企业开放服务类目一览表
  19. learn.log - 进程管理器fastcgi原理及fastcgi_param详解
  20. 什么是抖音小程序 小程序该怎么做

热门文章

  1. 9/6字节校招研发岗位笔试
  2. Spark RDD API:Map和Reduce
  3. MySQL:实用 SQL 语句集合
  4. redis学习之常用数据类型
  5. 你的应用是如何被替换的,App劫持病毒剖析
  6. hdu 2295 Radar 重复覆盖+二分
  7. 《Cracking the Coding Interview》——第18章:难题——题目11
  8. C# ListT用法
  9. 21位花朵数 C语言(执行时间小于16s)
  10. c# BindingSource的简单应用