USACO 2.3.1 Longest Prefix --- Trie

该博客介绍了如何使用Trie数据结构解决USACO中找到最长公共前缀的问题。通过C++实现Trie节点和插入、查找功能,读取输入数据并进行处理,最终输出最长公共前缀的长度。
摘要由CSDN通过智能技术生成
 /*
 PROG:   prefix
 ID  :   
 LANG:   C++
 */
 #include <string.h>
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
 #include <iostream>
 #include <memory.h>
 #include <algorithm>
 using namespace std;
 
 struct Trie_Node
 {
     bool IsEnd;
     Trie_Node *branch[26];
     Trie_Node(): IsEnd( false )
     {
         memset( branch, 0, sizeof(branch) );
     }// Init
 };
 
 class Trie
 {    
     public:
         Trie();
         void Trie_Insert( char tt[] );
         void Trie_Find( long j );
     
     private:
         Trie_Node *root;
 }t; // type class "Trie" object t
 
 long slen;
 bool prefix[200400];
 char line[204], ss[200400];
 
 Trie::Trie()
 {
     root = new Trie_Node();
 }// Trie
 
 void Trie::Trie_Insert( char tt[] )
 {
     Trie_Node *ptr = root;
     int tlen = strlen( tt );
     for ( int i=0; i<tlen; ++i )
     {
         if ( ptr->branc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值