HDU: 1247.Hat’s Words

来源:互联网 发布:mac文明5秘籍 编辑:程序博客网 时间:2024/06/06 07:36

Ctrl+Z结束输入

#include <stdio.h>#include <string.h>struct st{char word[100];}w[50010];const int maxnode = 1000000;const int sigma_size = 26;struct Trie {    int ch[maxnode][sigma_size];    int val[maxnode];    int sz;    void clear( ) { sz = 1; memset ( ch[0], 0, sizeof ( ch[0] ) ); }    int idx ( char c ) { return c - 'a'; }    void Insert ( char *s, int v ) {        int u = 0, n = strlen ( s );        for ( int i = 0; i < n; ++i ) {            int c = idx ( s[i] );            if ( !ch[u][c] ) {                memset ( ch[sz], 0, sizeof ( ch[sz] ) );                val[sz] = 0;                ch[u][c] = sz++;            }            u = ch[u][c];        }        val[u] = v;    }    bool find ( const char *s ) {int u = 0, n = strlen ( s );for ( int i = 0; i < n; ++i ) {int c = idx(s[i]);if ( !ch[u][c] ) return false;u = ch[u][c];}        if ( val[u] ) return true;        else return false;}}trie;int main ( ) {   trie.clear ();   int k=0,x,y,t;    while(scanf("%s",w[k].word)!=EOF){        trie.Insert ( w[k++].word, 1 );    }    for(int i=0;i<k;++i){        t=strlen(w[i].word);        for(int j=0;j<t;j++){            char a[100]={},b[100]={};            for(x=0;x<=j;++x)                a[x]=w[i].word[x];            for(y=0;x<t;++y,++x)                b[y]=w[i].word[x];           if(trie.find ( a )&&trie.find ( b )){                printf("%s\n",w[i].word);                break;            }        }    }    return 0;}


 

原创粉丝点击