当gets第一个遇到的字符是换行符时,就不往下读了,只能读到一个空串,为了读取下一个字符串,其实我们需要的应该是scanf("%s",s)
题目
#include<cstdio>
using namespace std;
struct node{
node *lc,*rc;
int e;
} nodes[110];
int cur=0;
node * newnode(int e){
node *t=&nodes[cur++];
t->e=e;
t->lc=t->rc=0;
return t;
}
node *insert(node *t,int e){
if(t==0) return newnode(e);
if(e< t->e) t->lc=insert(t->lc,e);
else t->rc=insert(t->rc,e);
return t;
}
bool comp(node *t1,node *t2){
if(t1==0&&t2==0) return 1;
if(t1==0||t2==0) return 0;
return (t1->e==t2->e)&&comp(t1->lc,t2->lc)&&comp(t1->rc,t2->rc);
}
void preord(node *t){
if(t==0) return;
printf("%d",t->e);
preord(t->lc); preord(t->rc);
}
char s[100];
int main(){
int n;
while(scanf("%d",&n)==1&&n){
//gets(s); 如果用gets,只能得到空串
scanf("%s",s);
//printf("%s\n",s);
node *t=0; cur=0;
for(int i=0;;++i){
if(s[i]==0) break;
t=insert(t,s[i]-'0');
}
//preord(t); printf("\n");
int tmpcur=cur;
for(int i=0;i<n;++i){
node *t1=0;
scanf("%s",s); cur=tmpcur;
for(int i=0;;++i){
if(s[i]==0) break;
t1=insert(t1,s[i]-'0');
}
//preord(t1); printf("\n");
if(comp(t,t1)) printf("YES\n");
else printf("NO\n");
}
}
return 0;
}
第34行错误