HD_1247Hat’s Words (字典树)

来源:互联网 发布:js给div设置id 编辑:程序博客网 时间:2024/06/04 18:42


Hat’s Words

                                             Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                            Total Submission(s): 11555    Accepted Submission(s): 4114


Problem Description
A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
You are to find all the hat’s words in a dictionary.
 

Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.
Only one case.
 

Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
 

Sample Input
aahathathatwordhzieeword
Sample Output
ahathatword
 



字典树题目,思路是将存进去的每个单词分开,然后将分开后的两个单词在树中查找,如果找到就输出。最后可以把节点清除掉,代码如下:


#include <cstdio>#include <cstring>#include <cstdlib>using namespace std;#define MAX 26struct Trie{    bool isWord;    struct Trie *next[MAX];    Trie(){    isWord=false;    memset(next,0,sizeof(next));    }};Trie *root=NULL; char s[50000][50]; void insert(char *word)      //插入单词 {     Trie *p=root;    while(*word != '\0')     {         if(p->next[*word-'a'] == NULL)        {                         Trie *temp=new Trie();            p->next[*word-'a']=temp;        }           p=p->next[*word-'a'];        word++;    }      p->isWord=true;}  bool search(char *word)         //查找单词是否存在 {    Trie *p=root;    for(int i=0; word[i]!='\0'; i++)    {        if(p->next[word[i]-'a']==NULL)             return false;        p=p->next[word[i]-'a'];    }    return p->isWord;} void del(Trie *root)                    //释放空间 {    for(int i=0;i<MAX;i++)    {       if(root->next[i]!=NULL)        {            del(root->next[i]);        }    }    delete(root);}int main(){char ch[50];int count=0;root=new Trie();while(scanf("%s",ch)!=EOF){strcpy(s[count++],ch);insert(ch);}for(int i=0;i<count;++i){ for(int j=1;j<strlen(s[i]);++j){ char ch1[50]={'\0'};char ch2[50]={'\0'};strncpy(ch1,s[i],j); strncpy(ch2,s[i]+j,strlen(s[i])-j);if(search(ch1)&&search(ch2)){ printf("%s\n",s[i]);break;}}}del(root);return 0;}


此题用到一个strncpy()一个c语言函数,简介如下:

strncpy()函数
原型:extern char *strncpy(char *dest, char *src, int n);    
用法:#include <string.h>    
功能:把src所指由NULL结束的字符串的前n个字节复制到dest所指的数组中。    
说明:如果src的前n个字节不含NULL字符,则结果不会以NULL字符结束。        
如果src的长度小于n个字节,则以NULL填充dest直到复制完n个字节。        
src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。        
返回指向dest的指针(该指向dest的最后一个元素)


在 ANSI C 中,strcpy 的安全版本是 strncpy。

char *strncpy(char *s1, const char *s2, size_t n);

但 strncpy 其行为是很诡异的(不符合我们的通常习惯)。标准规定 n 并不是 sizeof(s1),而是要复制的 char 的个数。一个最常见的问题,就是 strncpy 并不帮你保证 \0

结束。

char buf[8];
strncpy( buf, "abcdefgh", 8 );

看这个程序,buf 将会被 "abcdefgh" 填满,但却没有 \0 结束符了。

另外,如果 s2 的内容比较少,而 n 又比较大的话,strncpy 将会把之间的空间都用 \0 填充。这又出现了一个效率上的问题,如下:

char buf[80];
strncpy( buf, "abcdefgh", 79 );

上面的 strncpy 会填写 79 个 char,而不仅仅是 "abcdefgh" 本身。


strncpy 的标准用法为:(手工写上 \0)

strncpy(path, src, sizeof(path) - 1);
path[sizeof(path) - 1] = '\0';
len = strlen(path);




0 0
原创粉丝点击