zoukankan      html  css  js  c++  java
  • HDU1247

    字典树

    关键在于怎样找出两个单词拼成的单词,可以用strncpy函数

    字典树标记了单词的结尾

    View Code
     1 /*
     2 字典树
     3 */
     4 #include<stdio.h>
     5 #include<stdlib.h>
     6 #include<string.h>
     7 #include<iostream>
     8 #include<algorithm>
     9 #include<queue>
    10 #include<map>
    11 #include<math.h>
    12 using namespace std;
    13 const int maxn = 55005;
    14 const int inf = 0x7fffffff;
    15 struct tree{
    16     int lev;
    17     tree *next[ 26 ];
    18 };
    19 tree root;
    20 void init(){
    21     for( int i=0;i<26;i++ )
    22         root.next[ i ]=NULL;
    23 }
    24 
    25 void creat_tree( char s[] ){
    26     int len=strlen( s );
    27     tree *p=&root,*tmp;
    28     
    29     for( int i=0;i<len;i++ ){
    30         int id=s[ i ]-'a';
    31         if( p->next[ id ]==NULL ){
    32             tmp=( tree *)malloc(sizeof(root));
    33             /*
    34             if( i!=len-1 )
    35                 tmp->lev=-1;
    36             else 
    37                 tmp->lev=1;
    38                 */
    39             tmp->lev=-1;
    40             for( int j=0;j<26;j++ )
    41                 tmp->next[ j ]=NULL;
    42             p->next[ id ]=tmp;
    43             p=p->next[ id ];
    44         }
    45         else{
    46             /*
    47             if( i!=len-1 )
    48                 p->next[id]->lev=-1;
    49             else 
    50                 p->next[id]->lev=1;
    51             */
    52             //(p->next[id])->lev=-1;
    53             p=p->next[id];
    54         }
    55     }
    56     p->lev=1;
    57     return ;
    58 }
    59 
    60 int find( char s[] ){
    61     int len=strlen( s );
    62     tree *p=&root;
    63     for( int i=0;i<len;i++ ){
    64          int id=s[i]-'a';
    65          if( p->next[id]==NULL ) return -1;
    66          p=p->next[ id ];
    67     }
    68     if( p->lev==1 ) return 1;
    69     else return -1;
    70 }
    71 
    72 char s[ maxn ][ 105 ];
    73 int main(){
    74     int cnt=0;
    75     init();
    76     //int n;
    77     while( scanf("%s",s[ cnt ])!=EOF ){
    78         creat_tree( s[ cnt ] );
    79         cnt++;
    80     }
    81     char a[ 105 ],b[ 105 ];
    82     for( int i=0;i<cnt;i++ ){
    83         //printf("i:%d \n",i);
    84         int len=strlen( s[i] );
    85         for( int j=0;j<len;j++ ){
    86             memset( a,'\0',sizeof(a) );
    87             memset( b,'\0',sizeof(b) );
    88             strncpy( a,s[i],j );
    89             strncpy( b,s[i]+j,len-j );
    90             //printf("a:%s@b:%s\n",a,b);
    91             if( find( a )==1 && find( b )==1 ){
    92                 printf("%s\n",s[i]);
    93                 break;
    94             }
    95         }
    96     }
    97     return 0;
    98 }
    keep moving...
  • 相关阅读:
    浅析Python闭包
    1、Spring framework IOC容器
    springcloud 返回流案例
    foreach使用场景
    Iterator遍历集合时不可以删除集合中的元素问题
    Spring整合MyBatis配置
    Mybatis案例配置
    interrupt() interrupted() isInterruped() 区别
    4.对象关系 Map(ORM)数据访问
    3.使用 JDBC 进行数据访问
  • 原文地址:https://www.cnblogs.com/xxx0624/p/2811981.html
Copyright © 2011-2022 走看看