After trying to solve problem EDIT1(Editor) and being ****ed by Brainf**k, Blue Mary decided to set another difficult problem about editor.
Some definations:
Editor is a structure.It contains one text and one cursor.The operations are listed below:
--------------------------------------------------------------------------
| Move(k) | Move k | Move the cursor after the kth character |
| | | in the text. If k=0, you should put |
| Insert(n,s) | Insert n s | Insert string s whose length is n(>=1) |
| Delete(n) | Delete n | Delete n(>=1) characters after the cursor.|
If the text of a editor is empty,we say the editor is empty.
Here is an example._ denotes to the cursor,$ denotes to the start and the end.At start the editor is empty.
------------------------------------------------------------------------------
Your task is:
the very first line contains the number of testcases T(T<=4).T tests follow.
For each test, the first line is the number of operations N.N operations follow.
Blue Mary is so depressed with the problem EDIT1 that she decides to make the problem more difficult.So she inserts many extra line breaks in the string of the Insert operation.You must ignore them.
Except line breaks, all the charaters' ASCII code are in [32,126]. There's no extra space at the end of a line.
You can assume that for each test case:
The output should contain T blocks corresponding to each testcase.
For each test case, the output should contain as many lines as the get operations in the input.Each line should contains the output of each get operation.
Input: 1
15
Insert 26
abcdefghijklmnop
qrstuv wxy
Move 15
Delete 11
Move 5
Insert 1
^
Next
Insert 1
_
Next
Next
Insert 4
.\/.
Get 4
Prev
Insert 1
^
Move 0
Get 22 Output:
.\/.
abcde^_^f.\/.ghijklmno
Warning: large Input/Output data, be careful with certain languages
Blue Mary's note: the test case #1 has something wrong and it has been fixed on April 27th, 2007.Solutions has been rejudged. Please accept my apology.
题目取自SPOJ
几点注意的:
1、bzoj样例有误。
2、Insert操作如果读入长度用scanf("%d\n",&x)读,会自动过滤下一行空格,导致Wa90.
相信这是我写过最差的程序了。
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define MAXN MAXT
#define MAXT 1024*1024*4+1000
int n,m;
struct Splay_tree
{
struct node
{
node *ch[],*fa;
char w;
int siz;
};
node E[MAXN],nil_node;
queue
node *root,*nil;
Splay_tree()
{
int i;
for (i=;i
}
void update(node *now)
{
if (now==nil)throw "illegal update";
now->siz=now->ch[]->siz+now->ch[]->siz+;
}
void rotate(node *now,int p)//注意不要改变nil的值,now的祖父节点或儿子节点可能为nil
{
node *pnt=now->fa;
now->fa=pnt->fa;
if (pnt->fa!=nil)
{
if (pnt->fa->ch[]==pnt)
{
pnt->fa->ch[]=now;
}else
{
pnt->fa->ch[]=now;
}
}
pnt->fa=now;
pnt->ch[p^]=now->ch[p];
if (now->ch[p]!=nil)now->ch[p]->fa=pnt;
now->ch[p]=pnt;
update(pnt);//注意顺序
update(now);
}
void splay(node *now,node *top)
{
node *pnt;
if (now==top)return ;
while (now->fa!=top)
{
pnt=now->fa;
if (pnt->ch[]==now)
{
if (pnt->fa!=top&&pnt->fa->ch[]==pnt)
{
rotate(pnt,);
}
rotate(now,);
}else
{
if (pnt->fa!=top&&pnt->fa->ch[]==pnt)
{
rotate(pnt,);
}
rotate(now,);
}
}
if (top==nil)
{
root=now;
}
}
node *get_node(int rank)
{
node *now=root;
if (now->siz
{
return now;
}
if (now->ch[]->siz+
now=now->ch[];
}else
{
now=now->ch[];
}
}
return now;
}
node *get_min_node(node *now)
{
if (now==nil)throw "illegal call";
//if (now==nil)return nil;
while (now->ch[]!=nil)
{
now=now->ch[];
}
return now;
}
pair
{
if (pos==)return make_pair(nil,root);
splay(get_node(pos),nil);
pair
ret.first=root;
ret.second=root->ch[];
root->ch[]->fa=nil;
root->ch[]=nil;
update(root);
root=NULL;
return ret;
}
node * merge(node * a1,node *a2)
{
if (a1==nil)return a2;
if (a2==nil)return a1;
root=a2;
splay(get_min_node(a2),nil);
root->ch[]=a1;
a1->fa=root;
update(root);
return root;
}
void insert(int pos,char ch)//插入ch后前面有pos个字符
{
node *now=Q.front();
Q.pop();
now->w=ch;
if (pos==)
{
if (root==nil)
{
now->fa=nil;
now->ch[]=now->ch[]=nil;
now->siz=;
root=now;
return ;
}
splay(get_min_node(root),nil);
now->fa=root;
root->ch[]=now;
now->ch[]=now->ch[]=nil;
update(now);
update(root);
return ;
}
splay(get_node(pos),nil);
if (root->ch[]!=nil)splay(get_min_node(root->ch[]),root);
now->fa=root;
now->ch[]=root->ch[];
root->ch[]->fa=now;
root->ch[]=now;
now->ch[]=nil;
update(now);
update(root);
splay(now,nil);
return ;
}
void insert2(int pos,char* str,int len)
{
if (len==)return ;
pair
pr1=split(pos);
//scan(pr1.first);cout<
node *now=Q.front();
int mid=(l+r)/;
Q.pop();
now->fa=fa;
now->w=str[mid];
now->ch[]=build_tree(str,l,mid-,now);
now->ch[]=build_tree(str,mid+,r,now);
update(now);
return now;
}
void recycle(node *now)
{
if (now==nil)return ;
if (now->fa!=nil)
{
if (now==now->fa->ch[])
{
now->fa->ch[]=nil;
}else if (now==now->fa->ch[])
{
now->fa->ch[]=nil;
}
}
recycle(now->ch[]);
recycle(now->ch[]);
Q.push(now);
}
void erase(int pos,int len)
{
pair<node\*,node \*> pr1,pr2;
pr1=split(pos);
root=pr1.second;
pr2=split(len);
recycle(pr2.first);
root=merge(pr1.first,pr2.second);
}
void scan(node \*now)
{
if (now==nil)return;
if (now->siz!=now->ch\[\]->siz+now->ch\[\]->siz+)
{
throw "Size error";
}
if (now->ch\[\]!=nil&&now->ch\[\]->fa!=now)throw "Wrong ptr";
if (now->ch\[\]!=nil&&now->ch\[\]->fa!=now)throw "Wrong ptr";
scan(now->ch\[\]);
printf("%c",now->w);
scan(now->ch\[\]);
}
void scan2(node \*now)
{
if (now==nil)return;
scan2(now->ch\[\]);
printf("%c",now->w);
scan2(now->ch\[\]);
}
void print\_str(int pos,int len)
{
if (!len){puts("");return ;}/\*\*/
if (pos==)
{
if (len==root->siz)
{
scan(root);
puts("");
return ;
}
splay(get\_node(len+),nil);
scan(root->ch\[\]);
puts("");
return ;
}
splay(get\_node(pos),nil);
if (pos+len<=root->siz)splay(get\_node(pos+len),root);
node \*temp=root->ch\[\],\*temp2=root->ch\[\]->ch\[\];
root->ch\[\]=nil;root->ch\[\]->ch\[\]=nil;
scan2(root->ch\[\]);puts("");
root->ch\[\]=temp;root->ch\[\]->ch\[\]=temp2;
return ;
}
}spt;
int i;
char str1[MAXN];
int main()
{
//freopen("editor2.in","r",stdin);
//freopen("out2.txt","w",stdout);
try
{
int j,k,x,y;
scanf("%d",&n);
char od[];
int m,p;
char *ptr;
char ch;
int nowad=;
int root2;
for (i=;i<n;i++)
{
scanf("%s ",od);
switch (od[])
{
case 'M':
scanf("%d\\n",&x);
nowad=x;
break;
case 'I':
scanf("%d",&x);
getchar();
for (j=;j<x;j++)
{
ch=getchar();
if (ch<||ch>)
{
j--;
continue;
}
str1\[j\]=ch;
//if (i!=1)cerr<<ch<<endl;;
//spt.insert(nowad+j,ch);
}
str1\[x\]='\\0';
spt.insert2(nowad,str1,x);
if (x)ch=getchar();
break;
case 'D':
scanf("%d\\n",&x);
spt.erase(nowad,x);
break;
case'G':
scanf("%d\\n",&x);
spt.print\_str(nowad,x);
break;
case'P':
nowad--;
break;
case'N':
nowad++;
break;
}
/\* cout<<od<<" "<<x<<endl;
if (od\[0\]=='I')cout<<str1<<endl;
cout<<"<<";spt.scan(spt.root);cout<<"\["<<nowad<<"\]";
cout<<endl;\*/
}
}catch (const char\* err)
{
cout<<err;
return ;
}
return ;
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章