hdu1247字典树

来源:互联网 发布:淘宝翡翠手镯 编辑:程序博客网 时间:2024/05/20 01:47

Hat’s Words

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


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
 
#define DeBUG#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <string>#include <set>#include <sstream>#include <map>#include <bitset>using namespace std ;#define zero {0}#define INF 2000000000#define EPS 1e-6typedef long long LL;const double PI = acos(-1.0);//#pragma comment(linker, "/STACK:102400000,102400000")inline int sgn(double x){    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);}struct Trie{    Trie *next[26];    bool flag;//标记当前单词是否有效,比如有aabb,查aa就不有效    Trie()    {        flag = false;        for (int i = 0; i < 26; i++)            next[i] = NULL;    }};Trie root;void createTrie(char *str){    int len = strlen(str);    Trie *p = &root, *q;    for (int i = 0; i < len; i++)    {        int id = str[i] - 'a';        if (p->next[id] == NULL)        {            q = (Trie *)malloc(sizeof(root));            for (int j = 0; j < 26; j++) q->next[j] = NULL;                q->flag=false;            p->next[id] = q;            p = p->next[id];        }        else        {            p = p->next[id];        }    }    p->flag = true;}bool findit(char *s){    int len = strlen(s);    Trie *p = &root;    int id;    for (int i = 0; i < len; i++)    {        id = s[i] - 'a';        if (p->next[id] != NULL)        {            p = p->next[id];        }        else        {            return false;        }    }    return p->flag;}void del(Trie *head){    for (int i = 0; i < 26; i++)    {        if (head->next[i] != NULL)            del(head->next[i]);    }    delete(head);}void clear(){    del(&root);    for (int i = 0; i < 26; i++)    {        root.next[i] = NULL;    }}char s[50000][100] = zero;string lins[50000];int main(){#ifdef DeBUGs    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);    // freopen("C:\\Users\\Sky\\Desktop\\dabiao.txt","w",stdout);#endif    int n = 0;    while (scanf("%s", s[n]) + 1)    {        createTrie(s[n++]);    }    bool flag = true;    int l;    char ss[100];    int k = 0;    int num = 0;    for (int i = 0; i < n; i++)    {        flag = true;        memset(ss, 0, sizeof(ss));        l = strlen(s[i]);        k = 0;        for (int j = 0; j < l - 1; j++)        {            ss[j] = s[i][j];            if (findit(ss) && findit(&s[i][j + 1]))            {                lins[num++] = string(s[i]);                break;            }        }    }    for (int i = 0; i < num; i++)    {        printf("%s\n", lins[i].c_str());    }    return 0;}


0 0
原创粉丝点击