付翔的专栏
在鄙视中成长 记录成长的点滴
posts - 106,  comments - 32,  trackbacks - 0
#include<stdio.h>
#include
<string.h>
#include
<ctype.h>
struct TreeNode //结点
{
    
char EN_word[25];
    TreeNode 
*next[26];
    TreeNode()
//构造函数做初始化
    {
        EN_word[
0]='\0';
        
for(int i=0;i<26;i++) next[i]=NULL;
    }
    
~TreeNode()//析构函数做善后工作
    {
        
for(int i=0;i<26;i++)
            
if(next[i]!=NULL) delete next[i];
    }
};
void insert(TreeNode *&root,const char *EN_word,const char * MA_word)//插入结点
{
    TreeNode 
* loca=root;
    
int i=0,ban=0;
    
if(loca==NULL){loca = new TreeNode();root=loca;}
    
int len=strlen(MA_word);
    
while(MA_word[i])
    {
        ban
=MA_word[i]-'a';
        
if(!loca->next[ban])
        {
            loca
->next[ban]=new TreeNode();
            
if(i==len-1)//判断是否最后一个字符,附加信息放在这里
                strcpy(loca->next[ban]->EN_word,EN_word);
        }
        i
++;
        loca
=loca->next[ban];
    }
}
bool search(TreeNode *&root,const char * MA_word)
{
    TreeNode 
* loca=root;
    
int i=0,ban=0;
    
char ans[25];
    
int len=strlen(MA_word);
    
if(MA_word[0== ' 'return 0;
    
if(MA_word[0== '\n'return 0;
    
while(MA_word[i])
    {
        ban
=MA_word[i]-'a';
        
if(!loca->next[ban])
            
return 0;
        loca 
= loca->next[ban];
        i 
++;
    }

    strcpy(ans,loca
->EN_word);
    
if(ans[0== '\0'return 0;
    printf(
"%s",ans);
    
return 1;
}
int main()
{
    freopen(
"in.txt","r",stdin);
    
int n,i;
    
char c;
    
char str1[25],str2[25];
    
struct TreeNode *headNode = NULL;
    
while(true)
    {
        scanf(
"%s",str1);
        
if(strcmp(str1,"END")==0)  break;
        
if(strcmp(str1,"START")==0continue;
        scanf(
"%s",str2);
        insert(headNode,str1,str2);

    }
    getchar();
    gets(str2);
    i 
= 0;
    
while(true)
    {
        c 
= getchar();
        
if(c =='E'break;
        
/*while((c=getchar() ) && islower(c))
            str1[++i] = c;
*/
        
while(1)// 只是这里在处理字符串的时候 样例过了 但是提交报内存错误
        {
            str1[i
++= c;
            c 
= getchar();
            
if(!islower(c))
            {
                str1[i] 
= '\0';
                
break;
            }
        }
        
if(search(headNode,str1)==0) printf("%s",str1);
        
if(c != 'E')printf("%c",c);
        
if(c =='E'break;
        i 
= 0;
        
    }
    
return 0;
}

下面的是正确的
#include<stdio.h>
#include
<string.h>
#include
<ctype.h>
struct TreeNode //结点
{
    
char EN_word[25];
    TreeNode 
*next[26];
    TreeNode()
//构造函数做初始化
    {
        EN_word[
0]='\0';
        
for(int i=0;i<26;i++) next[i]=NULL;
    }
    
~TreeNode()//析构函数做善后工作
    {
        
for(int i=0;i<26;i++)
            
if(next[i]!=NULL) delete next[i];
    }
};
void insert(TreeNode *&root,const char *EN_word,const char * MA_word)//插入结点
{
    TreeNode 
* loca=root;
    
int i=0,ban=0;
    
if(loca==NULL){loca = new TreeNode();root=loca;}
    
int len=strlen(MA_word);
    
while(MA_word[i])
    {
        ban
=MA_word[i]-'a';
        
if(!loca->next[ban])
        {
            loca
->next[ban]=new TreeNode();
            
if(i==len-1)//判断是否最后一个字符,附加信息放在这里
                strcpy(loca->next[ban]->EN_word,EN_word);
        }
        i
++;
        loca
=loca->next[ban];
    }
}
bool search(TreeNode *&root,const char * MA_word)
{
    TreeNode 
* loca=root;
    
int i=0,ban=0;
    
char ans[25];
    
int len=strlen(MA_word);
    
if(MA_word[0== ' 'return 0;
    
if(MA_word[0== '\n'return 0;
    
while(MA_word[i])
    {
        ban
=MA_word[i]-'a';
        
if(!loca->next[ban])
            
return 0;
        loca 
= loca->next[ban];
        i 
++;
    }

    strcpy(ans,loca
->EN_word);
    
if(ans[0== '\0'return 0;
    printf(
"%s",ans);
    
return 1;
}
int main()
{
    freopen(
"in.txt","r",stdin);
    
int n,i;
    
char c;
    
char str1[25],str2[25];
    
struct TreeNode *headNode = NULL;
    
while(true)
    {
        scanf(
"%s",str1);
        
if(strcmp(str1,"END")==0)  break;
        
if(strcmp(str1,"START")==0continue;
        scanf(
"%s",str2);
        insert(headNode,str1,str2);

    }
    getchar();
    gets(str2);
    i 
= 0;
    
while(true)
    {
        c 
= getchar();
        
if(c =='E'break;
        
/*while((c=getchar() ) && islower(c))
            str1[++i] = c;
*/
        str1[i
++= c;
        
if(c < 'a' || c > 'z')
        {
            str1[i
-1= '\0';
            
if(search(headNode,str1)==0) printf("%s",str1);
            
if(c != 'E')printf("%c",c);
            
             i 
= 0;
        }
        
        
//i++;
        
    }
    
return 0;
}

posted on 2010-07-19 10:21 付翔 阅读(91) 评论(0)  编辑 收藏 引用 所属分类: ACM 数据结构

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2010年7月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

CSDN - 我的blog地址

博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜