Poj 2804 Dictionary (Dictionary tree or quick row + binary), poj2804

Source: Internet
Author: User

Poj 2804 Dictionary (Dictionary tree or quick row + binary), poj2804
2804: Dictionary


Total time limit:
3000 ms
Memory limit:
65536kB
Description
You traveled to a foreign city. You cannot understand the Foreign Languages people speak there. Fortunately, you have a dictionary to help you.
Input
First, enter a dictionary containing no more than 100000 entries. Each entry occupies one row. Each entry contains an English word and a foreign language word. The two words are separated by a space. In addition, no foreign language word appears more than twice in the dictionary. A dictionary is followed by a blank line. A document consists of a foreign language word. The document contains no more than 100000 lines and each line contains only one foreign language word. The input contains only lowercase letters and cannot exceed 10 characters in length.
Output
In the output, you need to translate the input document into English and output an English word in each line. If a foreign language word is not in the dictionary, it is translated into "eh ".
Sample Input
dog ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay
Sample output
catehloops
Prompt
The input is large. c I/O functions are recommended.


At the beginning of this question, I came up with a dictionary tree. Later, because of the special input of this question, I had never known where to create a new one. Later I saw other people's methods for processing input; all of a sudden, I came to the inspiration. I applied the previous template and submitted the code to the best practices. Then I submitted the code in our school's oj. I was overwhelmed by a piece of data... (The data for best practices is a bit watery.) later I tried to solve the problem. The program at the beginning will output the result if it matches the part of the given word until the end of the given word, however, there is still a dictionary tree that does not fully match. At that time, this situation was not taken into account because of a; it was stuck here all the time; today I saw the code of other people's ac automatic machines; I also gave me a prompt.
Set a mark for the completion of a word in the node. After this process, I thought about it for a long time, but it was still a bit watery. The exception had always ended. Later, I finally wrote it, I think it is still a little opportunistic. Both oj have a, but it can be optimized. Sometimes there are still problems, and I don't know why;
The following is the code. It can be ac-optimized !!
# Include <cstdio> # include <cstring> # include <cstdlib> typedef struct node // The struct of the node {char eng [12]; int count; // mark whether the word ends struct node * next [26];} node; int flag; void Insert (node * T, char * f, char * e) // insert {node * p, * q; p = T; int len, k; len = strlen (f); if (len = 0) return; for (int I = 0; I <len; I ++) {k = f [I]-'A'; if (p-> next [k] = NULL) {q = (node *) malloc (sizeof (node); // Add a new node for (int I = 0; I <26; I ++) {q-> count = 0; str Cpy (q-> eng, e); q-> next [I] = NULL;} p-> next [k] = q; p = q ;} else p = p-> next [k];} p-> count ++;} void Search (node * T, char * s) // find {node * q; q = T; int k, I = 0, len; int flag = 0; for (I = 0; I <26; I ++) {k = s [I]-'A'; q = q-> next [k]; if (q = NULL) {flag = 1; printf ("eh \ n"); break;} if (q-> count> 0) // The End mark of the word {printf ("% s \ n ", q-> eng); flag = 1; break ;}} void Release (node * T) // destroy {for (int I = 0; I <26; I ++) if (T-> next [I]! = NULL) Release (T-> next [I]); free (T);} int main () {// freopen ("1.txt"," r ", stdin ); char english [20], forigen [20]; node * T; T = (node *) malloc (sizeof (node); T-> count = 0; for (int I = 0; I <26; I ++) T-> next [I] = NULL; while (1) {english [0] = getchar (); if (english [0] = '\ n') break; scanf ("% s", english + 1, forigen); Insert (T, forigen, english ); getchar () ;}while (scanf ("% s", forigen )! = EOF) {flag = 0; Search (T, forigen);} Release (T); return 0 ;}
The first ac Water Code;
Void Search (char * f) {node * q; int len, k; q = T; len = strlen (f); for (int I = 0; I <len; I ++) {k = f [I]-'A'; q = q-> next [k]; if (q = NULL) {printf ("eh \ n"); flag = 1; break ;}} if (flag = 0) printf ("% s \ n", q-> eng ); // some matching results are output directly}
The dictionary tree code written by others is so clever and worth learning. The last character of a word is processed separately and marked to avoid mistakes I made earlier. I read others' codes, mouseton;
# Include "stdio. h "# include" string. h "# include" math. h "# include" algorithm "# include" iostream "using namespace std; typedef struct node {struct node * next [26]; char ans [12];} node; char arr1 [12], arr2 [12], str [25]; node * T; void Diction () {node * q, * p = T; int I, j, k, len; len = strlen (arr2); for (I = 0; I <len-1; I ++) {k = arr2 [I]-'A '; if (p-> next [k] = NULL) {q = (node *) malloc (sizeof (node); p-> next [k] = q; q-> ans [0] = 0; for (j = 0; j <26; j ++) q-> next [j] = NULL; p = q;} else {p = p-> next [k];} k = arr2 [I]-'A'; // The last character if (p-> next [k] = NULL) {q = (node *) malloc (sizeof (node); p-> next [k] = q; strcpy (q-> ans, arr1 ); // copy the arr array to the node of the last character. Other nodes are 0 for (j = 0; j <26; j ++) q-> next [j] = NULL;} else {p = p-> next [k]; strcpy (p-> ans, arr1);} void search () // query {node * p = T; int I, len, k; len = strlen (str); for (I = 0; I <len-1; I ++) {k = str [I]-'A'; if (p-> next [k] = NULL) {print F ("eh \ n"); break;} else {p = p-> next [k];} if (I = len-1) // The last character {k = str [I]-'A'; if (p-> next [k] = NULL) {printf ("eh \ n");} else {p = p-> next [k]; if (p-> ans [0]! = 0) // Insert the puts (p-> ans); else printf ("eh \ n") ;}} int main () {// freopen ("input.txt", "r", stdin); int I; T = (node *) malloc (sizeof (node); for (I = 0; I <26; I ++) T-> next [I] = NULL; while (1) {gets (str); if (strcmp (str ,"") = 0) break; sscanf (str, "% s", arr1, arr2); Diction () ;}while (scanf ("% s", str )! = EOF) search (); return 0 ;}

Another way of thinking is to directly use the combination of fast sorting + binary, which is also very common. The fgets and sscanf functions are used in this question for the first time; I also used qsort and bsearch in the C language library, and then I wrote another binary statement. It is much less difficult than writing a dictionary tree, and I still cannot grasp many details;
The following is the ac code, which is written directly by calling the library function;
# Include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> using namespace std; const int maxn = 100000 + 10; struct node // structure of the node {char eng [12]; char fore [12] ;}; node dictionary [maxn]; int cmp (const void *, const void * B) {// comparison function return strcmp (node *) a)-> fore, (node *) B)-> fore );} int search (const void * a, const void * B) {// binary search function return strcmp (char *) a, (node *) B) -> fore);} int main () {// freo Pen ("1.txt"," r ", stdin); char english [30], forigen [20]; int count = 0, flag; node * p; while (fgets (english, 29, stdin) & english [0]! = '\ N') {sscanf (english, "% s", dictionary [count]. eng, dictionary [count]. fore); count ++;} qsort (dictionary, count, sizeof (node), cmp); while (scanf ("% s", forigen )! = EOF) {p = NULL; p = (node *) bsearch (forigen, dictionary, count, sizeof (node), search); if (p) printf ("% s \ n", p-> eng); else printf ("eh \ n");} return 0 ;}
Write binary search by yourself;
# Include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> using namespace std; const int maxn = 100000 + 10; struct node // structure of the node {char eng [12]; char fore [12] ;}; node dictionary [maxn]; bool Cmp (node one, node two) {return strcmp (one. fore, two. fore) <0 ;}int bsearch (char * s, int n) // Binary Search {int mid, start, end; start = 0; end = n-1; int flag = 0; while (start <= end) {mid = (start + end)/2; flag = strcmp (s, dict Ionary [mid]. fore); if (flag = 0) return mid; if (flag <0) {end = mid-1;} else {start = mid + 1 ;}} return-1;} int main () {// freopen ("1.txt"," r ", stdin); char english [30], forigen [20]; int count = 0, flag; node * p; while (fgets (english, 29, stdin) & english [0]! = '\ N') {sscanf (english, "% s", dictionary [count]. eng, dictionary [count]. fore); count ++;} sort (dictionary, dictionary + count, Cmp); while (scanf ("% s", forigen )! = EOF) {flag = bsearch (forigen, count); if (flag! =-1) {printf ("% s \ n", dictionary [flag]. eng);} else printf ("eh \ n");} return 0 ;}
It is still necessary to accumulate more resources during normal exercise;
Summary: This question can be done in a variety of ways. In normal practice, you can try it more and familiarize yourself with the library functions. fgets and sscanf control the input format; the cmp Function Format in qsort, the search function Writing Method and format in bsearch are also used to transmit parameters. The dictionary tree must be flexibly processed based on the meaning of the question on the basis of the template, it is very convenient to set a parameter to indicate the end;
There are not many questions. They are excellent !!


 

Answer in the poj dictionary 2804

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Deprecision MAX 100001
Struct node
{
Char s [12];
Char t [12];
} Str [MAX];
Char a [12];
Int cmp (const void * a, const void * B) // fast sorting
{
Return strcmp (struct node *) a)-> t, (struct node *) B)-> t );
};
Void bsearch1 (char key [], int n) // binary method
{
Int low = 0, high = n-1, mid, f = 0;
While (low <= high)
{
Mid = (low + high)/2;
If (! Strcmp (str [mid]. t, key ))
{
F = 1;
Printf ("% s \ n", str [mid]. s );
}
If (strcmp (str [0]. t, str [n-1]. t) <0)
{
If (strcmp (str [mid]. t, key)> 0)
High = mid-1;
Else
Low = mid + 1;
}
Else
{
If (strcmp (key, str [mid]. t) <0)
Low = mid + 1;
Else
High = mid-1;
}
}
If (! F)
Printf ("eh \ n ");
}
Main ()
{
Int I = 0, j = 0, k = 0;
While (scanf ("% s", & str [I]. s )! = EOF) // read the string;
{
If (getchar () = '\ n ')
Break;
Else
{
Scanf ("% s", & str [I]. t );
++ I;
}
}
Qsort (str, I, sizeof (node), cmp );
Strcpy (a, str [I]. s); bsearch1 (a, I); // This case is considered separately, because s finally reads it.
While (scanf ("% s", )! = EOF)
{
Int f = 0;
Bsearch1 (a, I );
}
}... Remaining full text>
 

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.