HDU 1251 statistical difficulties (Dictionary tree template question | map application)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1251


Problem descriptionignatius recently encountered a problem. The teacher gave him many words (only lowercase letters are used and no duplicate words will appear ), the teacher asks him to calculate the number of words prefixed with a certain string (the word itself is also its own prefix ).
The first part of input data is a word table. Each line has one word. The length of a word cannot exceed 10. These words represent words that the teacher gave to Ignatius for statistics. A blank line indicates the end of the word table. the second part is a series of questions. Each question in each row is a string.

Note: This question only contains a set of test data, which is processed until the end of the file.
Output provides the number of words prefixed with this string for each question.
Sample Input
bananabandbeeabsoluteacmbabbandabc
Sample output
2310


The Code is as follows:

# Include <cstdio> # include <cstring> # include <malloc. h ># include <iostream> using namespace STD; # define maxn 26 typedef struct trie {int V; // trie * Next [maxn] varies as needed; // next indicates the number of types of data on each layer. If only lowercase letters are used, the value 26 is required. // if the number is changed to uppercase or lowercase letters, the value 52 is used. If the number is added, it is 62} trie; trie root; void createtrie (char * Str) {int Len = strlen (STR); trie * P = & root, * q; for (INT I = 0; I <Len; I ++) {int id = STR [I]-'A '; if (p-> next [ID] = NULL) {q = (Trie *) Malloc (sizeof (Root); q-> V = 1; // initial v = 1 for (Int J = 0; j <maxn; j ++) q-> next [J] = NULL; P-> next [ID] = Q; P = p-> next [ID];} else {P-> next [ID]-> V ++; P = p-> next [ID] ;}// p-> V =-1; // if it is the end, change V to-1 to indicate} int findtrie (char * Str) {int Len = strlen (STR); trie * P = & root; for (INT I = 0; I <Len; I ++) {int id = STR [I]-'A'; P = p-> next [ID]; if (P = NULL) // if it is an empty set, return 0 is not saved as the prefix; // If (p-> V =-1 )// The character set contains the prefix // return-1;} return p-> V; // return-1; // This string is the prefix of a string in the character set} int dealtrie (trie * t) {// dynamic dictionary tree, sometimes out of memory, if (t = NULL) return 0; For (INT I = 0; I <maxn; I ++) {If (t-> next [I]! = NULL) dealtrie (t-> next [I]);} Free (t); Return 0;} int main () {char STR [15]; for (INT I = 0; I <maxn; I ++) root. next [I] = NULL; while (gets (STR) & STR [0]! = '\ 0') createtrie (STR); memset (STR, 0, sizeof (STR); While (scanf ("% s", STR )! = EOF) {int ans = findtrie (STR); printf ("% d \ n", ANS) ;}return 0 ;}


Next we will provide a write method using map (simple but time-consuming ):

#include <cstdio>#include <iostream>#include <map>#include <cstring>#include <string>using namespace std;int main(){    char str[17];    map<string, int> m;    while(gets(str))    {        int len = strlen(str);        if (!len)        {            break;        }        for(int i = len; i > 0; i--)        {            str[i] = '\0';            m[str]++;        }    }    while(gets(str))    {        cout<<m[str]<<endl;    }    return 0;}


HDU 1251 statistical difficulties (Dictionary tree template question | map application)

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.