Big String
Given a string of not more than 106 length, there are two operations:
1. Add a character in front of the first character Ch
2. Query what is the character of the K position
The total number of operations does not exceed 2000
If you simulate directly, the amount of data moved to the back is too large. If we divide, we can optimize and reduce the amount of movement. A very typical block array. The next point of the block array is a piece, not a. Here, integers are used instead of pointers.
Each block is an object. This problem is programmed with object-oriented programming.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <string>7#include <vector>8#include <Set>9#include <map>Ten#include <stack> One#include <queue> A#include <sstream> -#include <iomanip> - using namespacestd; thetypedefLong LongLL; - Const intinf=0x4fffffff; - Const intexp=1e-5; - Const intms=2100; + - structnode + { A intSize,next; at CharStr[ms]; - voidPushCharch) - { -str[size++]=ch; - } - voidInsertintPosCharch) in { - for(inti=size++;i>pos;i--) tostr[i]=str[i-1]; +str[pos]=ch; - } the * }nodes[ms]; $ Panax Notoginseng Chars[ -*MS]; - intsize,cnt,q; the + voidinput () A { thescanf"%s", S); +scanf"%d",&Q); - intlen=strlen (S); $Size= (int) sqrt (0.1+len+Q); $Cnt=0; -Nodes[cnt].size=0; - for(intI=0; i<len;i++) the { - if(nodes[cnt].size>=SIZE)Wuyi { thenodes[cnt].next=cnt+1; -Nodes[++cnt].size=0; Wu } - Nodes[cnt].push (S[i]); About } $nodes[cnt].next=-1; - } - - //the maximum capacity of a block is 2*size, and when it is full, it will be supplemented with a piece from the back . A voidUpdata (intID) + { the if(nodes[id].size<2*size)//we are in the interval after inserting a character in the update, so we want to leave a position - return ; $++CNT; the inti,j,k=nodes[id].size; the for(i=size,j=0; i<k;i++,j++) thenodes[cnt].str[j]=Nodes[id].str[i]; theNodes[cnt].size=J; -Nodes[id].size=SIZE; innodes[cnt].next=Nodes[id].next; thenodes[id].next=CNT; the } About the voidSolve () the { the intI,j,pos; + CharCmd[ms]; - for(i=0; i<q;i++) the {Bayiscanf"%s", cmd); the if(cmd[0]=='Q') the { -scanf"%d",&POS); - for(j=0;p os>nodes[j].size;j=nodes[j].next) thepos-=nodes[j].size; theprintf"%c\n", nodes[j].str[pos-1]); the } the Else - { thescanf"%s%d",cmd,&POS); the for(j=0;p os>nodes[j].size&&nodes[j].next!=-1; j=nodes[j].next) thepos-=nodes[j].size;94Nodes[j].insert (min (pos-1, nodes[j].size), cmd[0]); the Updata (j); the } the }98 } About - intMain ()101 {102 input ();103 solve ();104 return 0; the}
Big String block Array (or square split)