zoukankan      html  css  js  c++  java
  • Poj 2887-Big String Splay

     
     
     
    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

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

    题解:

    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("
    %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
    ",tree[z].val);
     99         }
    100         else
    101         {
    102             scanf("
    %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
  • 相关阅读:
    每日一小练——数值自乘递归解
    linux的webserver配置与管理——创建用户个人主页
    微价值:专訪《甜心爱消除》个人开发人员Lee,日入千元!
    四月二十五日,bugzilla for CentOS 安装
    【剑指offer】顺时针打印矩阵
    何从之
    Java实现 蓝桥杯VIP 基础练习 时间转换
    Java实现 蓝桥杯VIP 基础练习 时间转换
    Java实现 蓝桥杯VIP 基础练习 字符串对比
    Java实现 蓝桥杯VIP 基础练习 字符串对比
  • 原文地址:https://www.cnblogs.com/Var123/p/5275769.html
Copyright © 2011-2022 走看看