zoukankan      html  css  js  c++  java
  • USACO 2.3.1 Longest Prefix Trie

    View Code
      1 /*
      2 PROG:   prefix
      3 ID  :   ouyangyewei
      4 LANG:   C++
      5 */
      6 #include <string.h>
      7 #include <cstdio>
      8 #include <cstdlib>
      9 #include <cstring>
     10 #include <iostream>
     11 #include <memory.h>
     12 #include <algorithm>
     13 using namespace std;
     14 
     15 struct Trie_Node
     16 {
     17     bool IsEnd;
     18     Trie_Node *branch[26];
     19     Trie_Node(): IsEnd( false )
     20     {
     21         memset( branch, 0, sizeof(branch) );
     22     }// Init
     23 };
     24 
     25 class Trie
     26 {    
     27     public:
     28         Trie();
     29         void Trie_Insert( char tt[] );
     30         void Trie_Find( long j );
     31     
     32     private:
     33         Trie_Node *root;
     34 }t; // type class "Trie" object t
     35 
     36 long slen;
     37 bool prefix[200400];
     38 char line[204], ss[200400];
     39 
     40 Trie::Trie()
     41 {
     42     root = new Trie_Node();
     43 }// Trie
     44 
     45 void Trie::Trie_Insert( char tt[] )
     46 {
     47     Trie_Node *ptr = root;
     48     int tlen = strlen( tt );
     49     for ( int i=0; i<tlen; ++i )
     50     {
     51         if ( ptr->branch[ tt[i]-'A' ]==NULL )
     52         {
     53             Trie_Node *tmp = new Trie_Node();
     54             ptr->branch[ tt[i]-'A' ] = tmp;
     55         }
     56                         
     57         ptr = ptr->branch[ tt[i]-'A' ];
     58     }
     59                     
     60     ptr->IsEnd = true;
     61 }// Trie_Insert
     62 
     63 void ReadData()
     64 {    
     65     while ( gets( line ) )
     66     {
     67         if ( line[0]=='.' ) break;
     68         
     69         char *tok = strtok( line, " " );
     70         while ( tok )
     71         {
     72             t.Trie_Insert( tok );
     73             //printf("%s\n", tok);
     74             tok = strtok( NULL, " " );
     75         }// Insert
     76     }
     77     
     78     char str[204];
     79     while ( gets( str ) )
     80     {
     81         strcat( ss, str );
     82     }
     83     
     84     slen = strlen( ss );
     85     
     86     return ;
     87 }// ReadData
     88 
     89 void Trie::Trie_Find( long j )
     90 {
     91     Trie_Node *ptr = root;
     92     for ( ; ; ++j )
     93     {
     94         ptr = ptr->branch[ ss[j]-'A' ];
     95         if ( ss[j]=='\0' || ptr==NULL )
     96             return ;
     97         if ( ptr->IsEnd )
     98             prefix[ j+1 ]=true;
     99     }
    100     
    101     return ;
    102 }// Trie_Find
    103 
    104 void Solve()
    105 {
    106     prefix[0] = true;
    107     for ( long i=0; i<slen; ++i )
    108     {
    109         if ( !prefix[i] )   continue;
    110         
    111         t.Trie_Find( i );
    112     }// Searching
    113     
    114     int result;
    115     for ( result=slen; !prefix[result]; --result );
    116     printf("%d\n", result);
    117 
    118     return ;
    119 }// Solve
    120 
    121 int main()
    122 {
    123     freopen("prefix.in", "r", stdin);
    124     freopen("prefix.out", "w", stdout);
    125 
    126     ReadData();
    127     Solve();
    128     
    129 //    system("pause");
    130     return 0;
    131 }
  • 相关阅读:
    514.栅栏染色
    OOM的起点到终点
    OOM的起点到终点
    2019 Android 高级面试题总结 从java语言到AIDL使用与原理 ...
    2019 Android 高级面试题总结 从java语言到AIDL使用与原理 ...
    Vue 结合 echarts 原生 html5 实现拖拽排版报表系统
    Vue 结合 echarts 原生 html5 实现拖拽排版报表系统
    Python一行代码获得IP地址
    Python一行代码获得IP地址
    记一次Pinpoint监控工具部署过程
  • 原文地址:https://www.cnblogs.com/yewei/p/2625745.html
Copyright © 2011-2022 走看看