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 ;}