数据结构:Rope

以BZOJ1507为例,这里仅仅展示动态区间问题的一些典型操作,包括插入删除和修改,查询的话不支持按顺序查询

使用起来很简单很方便

 1 #include<cstdio>
 2 #include<ext/rope>
 3 using namespace std;
 4 using namespace __gnu_cxx;
 5 crope list;
 6 int cur;
 7 char ch[3000005];
 8 inline int read()
 9 {
10     int x=0,f=1;char ch=getchar();
11     while(ch>'9'||ch<'0') {if(ch=='-')f=-1;ch=getchar();}
12     while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
13     return x*f;
14 }
15 int main()
16 {
17     int t,x;
18     t=read();
19     char s[10];
20     while(t--)
21     {
22         scanf("%s",s);
23         switch(s[0])
24         {
25             case 'M':cur=read();break;
26             case 'P':cur--;break;
27             case 'N':cur++;break;
28             case 'I':
29                 x=read();
30                 for(int i=0;i<x;i++)
31                 {
32                     ch[i]=getchar();
33                     while(ch[i]=='
') ch[i]=getchar();
34                 }
35                 ch[x]=0;
36                 list.insert(cur,ch);  //插入串 
37                 break;
38             case 'D':
39                 x=read();
40                 list.erase(cur,x);  //删除指定长度的串 
41                 break;
42             case 'G':
43                 x=read();
44                 list.copy(cur,x,ch);  //ch用来展示字符串 
45                 ch[x]=0;
46                 puts(ch);
47         }
48     }
49     return 0;
50 }

当然还有另外的三个函数,这里附上

    cout<<"test.replace(pos,x);//从pos开始换成x"<<endl;
    text.replace(1,'c');
    cout<<text<<endl;
    text.replace(1,"ccc");
    cout<<text<<endl<<endl<<endl;
 
    cout<<"test.substr(pos,x);//提取pos开始x个"<<endl;
    //text = text.substr(2);这样默认为提取一个
    cout<<text.substr(2)<<endl;
    cout<<text.substr(2,3)<<endl<<endl;
 
    cout<<"test.at(x)/[x];//访问第x个元素"<<endl;
    cout<<text.at(4)<<endl<<endl;
原文地址:https://www.cnblogs.com/aininot260/p/9512453.html