Noj 1121 Message Flood (trie tree or map)

Source: Internet
Author: User


Message Flood time limit (normal/java): 2000ms/6000ms running memory limit: 65536KByte
Total Submissions: 399 test pass:

Title Description

Well, how does the feel about mobile phone? Your answer would probably be something as that "It's so convenient and benefits people a lot". However, if you ask Merlin this question to the New year's Eve, he'll definitely answer "what a trouble! I had to keep my fingers moving on the phone the whole night, because I had so many greeting messages to send! ". Yes, Merlin has such a long name list of his friends, and he would like to send a greeting message to each of them. What's worse, Merlin has another long name list of senders that had sent message to him, and he doesn ' t want to send an Other message to bother them (Merlin are so polite, he always replies each message he receives immediately). So, the before he begins to the send messages, he needs to the figure of how many friends is left-to-be sent. Please write a him.
Here's something that should note. First, Merlin's friend list is not ordered, and each name is alphabetic strings andCase insensitive. These names is guaranteed to is not duplicated. Second, some senders may send more than one message to Merlin, therefore the sender list may be duplicated. Third, Merlin is known-many people, that's why some message senders was even not included on his friend list.


Input

There is multiple test cases. In each case, at the first line there was numbers n and M (1<=n, m<=20000), which is the number of friends a nd the number of messages he has received. And then there is n lines of alphabetic strings (the length of each would be is less than), indicating the names of Mer Lin ' s friends, one pre line. After that there is m lines of alphabetic string s, which is the names of message senders.
The input is terminated by n=0.

Output

For each case, print one integer in one line which indicates the number of the left friends he must send.

Sample input

5 3
Inkfish
Henry
Carp
Max
Jericho
Carp
Max
Carp
0

Sample output

3

Source of the topic

The ninth session of Zhongshan University Program design contest pre-selection


Title Link: http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1121


The main idea: a person to send a message to his n friends, of which already have m (May repeat) received, ask this person how much information to send


Title Analysis: If you use trie tree to pay attention to the meaning of red mark is not divided into the case, the establishment of 26-Fork Dictionary tree, Basic achievements inserted, ans initialized to N, find a time to find a way to delete it from the dictionary, with set + string to do a comparison, but the code is very good to write, very good understanding


Trie Tree:

#include <cstdio> #include <cstring>char s[11];int cnt, ans;int change (char ch) {if (ch <= ' Z ' &&    Ch >= ' a ') return CH-' a '; if (ch <= ' z ' && ch >= ' a ') return CH-' a ';    }struct node{node *next[26];    BOOL End;        Node () {memset (next, 0, sizeof (next));    end = false;        }};void Insert (node *p, char *s) {for (int i = 0; s[i]! = ' + '; i++) {int idx = change (s[i]);        if (P-next[idx] = = NULL) P--NEXT[IDX] = new node ();    p = P-NEXT[IDX]; } P-end = true;}    void Search (node *p, char *s) {int i;        for (i = 0; s[i]! = ' + '; i++) {int idx = change (s[i]);        if (P-next[idx] = = NULL) return;    p = P-NEXT[IDX];        } if (P-end) {ans--;    P-end = false;    }}int Main () {int n, m;        while (scanf ("%d", &n) && N) {ans = n;      Node *root = new node ();  scanf ("%d", &m);            for (int i = 0; i < n; i++) {scanf ("%s", s);        Insert (root, s);            } for (int i = 0; i < m; i++) {scanf ("%s", s);        Search (root, s);    } printf ("%d\n", ans); }}

Set + string:

#include <cstdio> #include <string> #include <iostream> #include <set>using namespace Std;int Main () {    int n, m;    String str;    while (scanf ("%d%d", &n, &m)! = EOF && N)    {        set <string> s;        for (int i = 0; i < n; i++)        {            cin >> str;            for (int j = 0; J < Str.length (); j + +)                str[j] = ToUpper (Str[j]);            S.insert (str);        }        for (int i = 0; i < m; i++)        {            cin >> str;            for (int j = 0; J < Str.length (); j + +)                str[j] = ToUpper (Str[j]);            Set <string>:: Iterator it = s.find (str);            if (it = S.end ())                s.erase (str);        }        cout << s.size () << Endl;    }}


Noj 1121 Message Flood (trie tree or map)

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.