分析:
典型的字典樹,只不過加了一個存字符串的指針。。。
注意格式的輸出。。。
[cpp] #include"stdio.h"
#include"string.h"
#include"stdlib.h"
struct tree
{
struct tree *child[26];
char *str;
};
struct tree *root;
char sstr[3111];
void insert(char *p,char *q)
{
int i,j;
int len;
struct tree *cur,*nee;
len=strlen(p);
cur=root;
for(i=0;i<len;i++)
{
if(cur->child[p[i]-'a']!=0)
cur=cur->child[p[i]-'a'];
else
{
nee=(struct tree*)malloc(sizeof(struct tree));
nee->str=NULL;
for(j=0;j<26;j++)
nee->child[j]=0;
cur->child[p[i]-'a']=nee;
cur=nee;
}
}
//
cur->str=new char[15];
strcpy(cur->str,q);
}
void find(char *p)
{
int i;
int len;
len=strlen(p);
struct tree *cur;
cur=root;
if(len==0)return;
for(i=0;i<len;i++)
{
if(cur->child[p[i]-'a']!=0&&p[i]>='a'&&p[i]<='z')
cur=cur->child[p[i]-'a'];
else
{
printf("%s",p);
return ;
}
}
if(cur->str!=NULL)printf("%s",cur->str);
else printf("%s",p);
}
int main()
{
int i;
char s[11],ss[11];
root=(struct tree*)malloc(sizeof(struct tree));
for(i=0;i<26;i++)
root->child[i]=0;
root->str=0;
while(scanf("%s",s))
{
if(strcmp(s,"START")==0)continue;
else if(strcmp(s,"END")==0)break;
else
{
scanf("%s",ss);
insert(ss,s);
}
}
getchar();
char str[3001],c[2];
while(gets(str))
{
if(strcmp(str,"START")==0)continue;
else if(strcmp(str,"END")==0)break;
else
{
int tt,t;
t=tt=0;
for(i=0;str[i];i++)
{
if(str[i]>='a'&&str[i]<='z')
{
if(tt==0)tt=1;
s[t++]=str[i];
}
else
{
if(tt==1)
{
tt=0;
s[t]=0;
t=0;
find(s);
}
printf("%c",str[i]);
}
}
if(tt==1)
{
s[t]=0;
find(s);
}
}
printf("\n");
}
return 0;
}
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
struct tree
{
struct tree *child[26];
char *str;
};
struct tree *root;
char sstr[3111];
void insert(char *p,char *q)
{
int i,j;
int len;
struct tree *cur,*nee;
len=strlen(p);
cur=root;
for(i=0;i<len;i++)
{
if(cur->child[p[i]-'a']!=0)
cur=cur->child[p[i]-'a'];
else
{
nee=(struct tree*)malloc(sizeof(struct tree));
nee->str=NULL;
for(j=0;j<26;j++)
nee->child[j]=0;
cur->child[p[i]-'a']=nee;
cur=nee;
}
}
//
cur->str=new char[15];
strcpy(cur->str,q);
}
void find(char *p)
{
int i;
int len;
len=strlen(p);
struct tree *cur;
cur=root;
if(len==0)return;
for(i=0;i<len;i++)
{
if(cur->child[p[i]-'a']!=0&&p[i]>='a'&&p[i]<='z')
cur=cur->child[p[i]-'a'];
else
{
printf("%s",p);
return ;
}
}
if(cur->str!=NULL)printf("%s",cur->str);
else printf("%s",p);
}
int main()
{
int i;
char s[11],ss[11];
root=(struct tree*)malloc(sizeof(struct tree));
for(i=0;i<26;i++)
root->child[i]=0;
root->str=0;
while(scanf("%s",s))
{
if(strcmp(s,"START")==0)continue;
else if(strcmp(s,"END")==0)break;
else
{
scanf("%s",ss);
insert(ss,s);
}
}
getchar();
char str[3001],c[2];
while(gets(str))
{
if(strcmp(str,"START")==0)continue;
else if(strcmp(str,"END")==0)break;
else
{
int tt,t;
t=tt=0;
for(i=0;str[i];i++)
{
if(str[i]>='a'&&str[i]<='z')
{
if(tt==0)tt=1;
s[t++]=str[i];
}
else
{
if(tt==1)
{
tt=0;
s[t]=0;
t=0;
find(s);
}
printf("%c",str[i]);
}
}
if(tt==1)
{
s[t]=0;
find(s);
}
}
printf("\n");
}
return 0;
}