SGU 271 水题。。。。 (三)

2014-11-24 01:02:45 · 作者: · 浏览: 8
x);
build(x->c[1],m+1,r,x);
x->up();
}
node *new_node(node *fa,char *s) {
node *x = &NODE[++top];
x->id = top;
x->c[0] = x->c[1] = null;
x->sz = 1;
x->flip = false;
strcpy(x->name,s);
x->fa = fa;
return x;
}
void ADD(char *s) {
RTO(1,null);
RTO(2,root);
node *tmp = new_node(root->c[1],s);
root->c[1]->setc(0,tmp);
root->c[1]->up();
root->up();
// // debug();
// puts("");
// print(root);
// puts("");
}
void ROTATE(int K) {
if(root->sz-2 <= K) {
RTO(1,null);
RTO(root->sz,root);
KT->flip ^= 1;
} else {
RTO(1,null);
RTO(K+2,root);
KT->flip ^= 1;
}
}
void print(node *x) {

if(x != null) {
x->down();
print(x->c[0]);
if(strcmp(x->name,"***")!=0) printf("%s\n",x->name);
print(x->c[1]);
}
}
void init(int n) {
for(int i = 0; i < n; i++) scanf("%s",num[i]);
root = new_node(null,"***");
root->c[1] = new_node(root,"***");
build(KT,0,n-1,root->c[1]);
root->c[1]->up();
root->up();
// debug();
}
/*
0 5 3
ADD(ABD)
ADD(XXX)
ADD(FDA)
ROTATE
ADD(wuyi)
*/
void Del_root() { // delete the root

node* t = root;
if (t->c[1] != null) {
root = t->c[1];
RTO(1, null);
root->c[0] = t->c[0];
if (root->c[0] != null)
root->c[0]->fa = root;
} else
root = root->c[0];
root->fa = null;
if (root != null)
root->up();
}
void vist(node *x) {
if (x != null) {
printf("节点:%2d : 左儿子: %2d 右儿子: %2d sz: %2d val: %s\n",
x->id,x->c[0]->id,x->c[1]->id,x->sz,x->name);
vist(x->c[0]);
vist(x->c[1]);
}
}
void debug() {
puts("************");
vist(root);
puts("\n*****************");
}
char num[maxn][5];
} spt;
void prepare() {
top = 0;
null->id = 0;
null->c[0] = null->c[1] = null->fa = NULL;
null->sz = 0;
null->flip = false;
strcpy(null->name,"***");
}
int main() {
prepare();
int n , m ,k;
scanf("%d%d%d",&n,&m,&k);
spt.init(n);
char op[20];
for(int i = 0; i < m; i++) {
scanf("%s",op);
if(strcmp(op,"ROTATE") == 0) {
spt.ROTATE(k);
} else {
int len = strlen(op);
char ta[10];
int head = 0;
for(int i = 4; i < len-1; i++) {
ta[head++] = op[i];
}
ta[head] = 0;
spt.ADD(ta);
}
}
spt.print(spt.root);
return 0;
}