6 条题解
-
0沉江底 LV 9 @ 2017-07-28 13:56:09
哇,这题写的都是泪啊,一开始想估计数据比较水就直接用树状数组模拟了,然后#4一直re,但是只跑了30ms,4千万点数组现在30ms就跑完了???然后放弃了直接用vector的。。。
#include <iostream> #include <vector> using namespace std; typedef struct node{ char value; int leftChild; int rightChild; }Node; vector<Node>tree; void A(int num){ if(num!=-1&&tree[num].value!=-1){ cout<<tree[num].value; A(tree[num].leftChild); A(tree[num].rightChild); } } void B(int num){ if(num!=-1&&tree[num].value!=-1){ B(tree[num].leftChild); cout<<tree[num].value; B(tree[num].rightChild); } } void C(int num){ if(num!=-1&&tree[num].value!=-1){ C(tree[num].leftChild); C(tree[num].rightChild); cout<<tree[num].value; } } int main(void){ char head,temp; int pos=0,end=0; bool flag=0; cin>>head; getchar(); Node* ptr=new Node; ptr->value=head; ptr->leftChild=-1; ptr->rightChild=-1; tree.push_back(*ptr); end++; cin>>temp; while((temp<='z'&&temp>='a')||temp=='0'){ if(tree[pos].value==-1){ while(tree[pos].value==-1){ pos++; } } if(temp=='0'){ temp=-1; } ptr=new Node; ptr->value=temp; ptr->leftChild=-1; ptr->rightChild=-1; tree.push_back(*ptr); if(flag){ tree[pos].rightChild=end; } else{ tree[pos].leftChild=end; } end++; flag=(flag==0?1:0); getchar(); if(!flag){ pos++; } cin>>temp; } getchar(); getchar(); cin>>head; if(head=='A'){ A(0); } else if(head=='B'){ B(0); } else{ C(0); } }
-
02015-10-16 15:06:59@
数据很水,直接从前到后扫一遍找父节点就行了,为什么AC的人那么少……
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;int i, j, k, l, m, n, lson[2015], rson[2015];
char a[2015], ch;void xxbl(int x){
if (a[x] != '0') putchar(a[x]);
if (lson[x] != -1) xxbl(lson[x]);
if (rson[x] != -1) xxbl(rson[x]);
}void zxbl(int x){
if (lson[x] != -1) zxbl(lson[x]);
if (a[x] != '0') putchar(a[x]);
if (rson[x] != -1) zxbl(rson[x]);
}void hxbl(int x){
if (lson[x] != -1) hxbl(lson[x]);
if (rson[x] != -1) hxbl(rson[x]);
if (a[x] != '0') putchar(a[x]);
}int main(){
scanf("%c", &a[1]); i = 2;
getchar();
for(j = 1; j <= 2015; j ++){lson[j] = -1; rson[j] = -1;}
while(true){
ch = getchar();
if (ch == '-') break;
if (ch != ' '){
a[i] = ch;
for(j = 1; j <= i; j ++){
if (lson[j] == -1 && a[j] != '0') {
lson[j] = i;
break;
}
else if (rson[j] == -1 && a[j] != '0'){
rson[j] = i;
break;
}
}
i ++;
}
}
getchar(); getchar();
ch = getchar();
if (ch == 'A') xxbl(1);
if (ch == 'B') zxbl(1);
if (ch == 'C') hxbl(1);
return 0;
} -
02014-07-09 14:23:02@
模拟水题
var lc,rc:array[1..20000] of longint;
a:array[1..20000] of char;
h,t:longint;
c:char;
procedure sea1(r:longint);
begin
if a[r]='0' then exit;
write(a[r]);
if lc[r]<>0 then sea1(lc[r]);
if rc[r]<>0 then sea1(rc[r]);
end;
procedure sea2(r:longint);
begin
if a[r]='0' then exit;
if lc[r]<>0 then sea2(lc[r]);
write(a[r]);
if rc[r]<>0 then sea2(rc[r]);
end;
procedure sea3(r:longint);
begin
if a[r]='0' then exit;
if lc[r]<>0 then sea3(lc[r]);
if rc[r]<>0 then sea3(rc[r]);
write(a[r]);
end;
begin
readln(a[1]);
h:=1;
t:=1;
read(c);
while c<>'-' do begin
if c=' ' then begin
read(c);
continue;
end;
inc(t);
a[t]:=c;
while a[h]='0' do inc(h);
if lc[h]=0 then begin
lc[h]:=t;
read(c);
continue;
end;
if rc[h]=0 then begin
rc[h]:=t;
inc(h);
read(c);
continue;
end;
end;
readln;
readln(c);
case c of
'A':sea1(1);
'B':sea2(1);
'C':sea3(1);
end;
writeln;
end. -
02012-11-15 15:30:13@
用数组来建树,注意0和边界情况~
-
02012-11-08 09:17:06@
注意要用Ansistring T^T
数组要开大数组要开大数组要开大数组要开大数组要开大
-
02012-10-24 16:20:56@
第一个发题解 液!
- 1