Poj 2503 babelfish (trie)

Source: Internet
Author: User
Http://poj.org/problem? Id = 2503

The idea is very simple. Create a trie tree for foreign language word and input the sequence number of the word at the time of insertion, which corresponds to the English word. Search for the serial number marked by word directly, and output englishword from the storage array.

What hurts a lot is the input of this question. At the beginning, I am really confused .. After this is done, it is 1y.

Code:

# Include <cstdio>
# Include <cstring>
Char STR [100001] [11]; // mode string
Char estr [100001] [11];
# Define Max 26 // character set size
Typedef struct trienode {
Int no;
Int count; // record the number of occurrences of this character
Struct trienode * Next [Max];
} Trienode;

Trienode memory [1000000];
Int allocp = 0;

/* Initialize */
Void inittrieroot (trienode ** proot ){
* Proot = NULL;
}

/* Create a new node */
Trienode * createtrienode (){
Int I;
Trienode * P;

P = & memory [allocp ++];
P-> COUNT = 1;
For (I = 0; I <Max; I ++ ){
P-> next [I] = NULL;
}
Return P;
}

/* Insert */
Void inserttrie (trienode ** proot, char * s, int no ){
Int I, K;
Trienode * P;
If (! (P = * proot )){
P = * proot = createtrienode ();
}
I = 0;
While (s [I]) {
K = s [I ++]-'A'; // confirm Branch
If (p-> next [k]) {
P-> next [k]-> count ++;
P-> next [k]-> NO = no;
}
Else {
P-> next [k] = createtrienode ();
P-> next [k]-> NO = no;
}
P = p-> next [k];
}
}

// Search
Int searchtrie (trienode ** proot, char * s ){
Trienode * P;
Int I, K;

If (! (P = * proot ))
Return-1;
I = 0;
While (s [I]) {
K = s [I ++]-'A ';
If (p-> next [k] = NULL) Return-1;
P = p-> next [k];
}
Return p-> NO;
}
Int main (){
Char s [11];
Int n = 0, I = 1;
Allocp = 0;
Trienode * root = NULL;
Inittrieroot (& root );
Scanf ("% C", & estr [I] [0]);
While (scanf ("% S % s", estr [I] + 1, s )){
Getchar ();
Inserttrie (& root, S, I ++ );
Scanf ("% C", & estr [I] [0]);
If (estr [I] [0] = '\ n') break;
}
While (~ Scanf ("% s", S )){
Int ans = searchtrie (& root, S );
If (ANS =-1) printf ("Eh \ n ");
Else printf ("% s \ n", estr [ANS]);
}
Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.