Message Flood-9th Sun Yat-sen University Program Design Competition qualifiers

Source: Internet
Author: User

Well, how do you feel about mobile phone? Your answer wowould probably be something like that "It's so convenient and benefits people a lot ". however, If you ask Merlin this question on the New Year's Eve, he will definitely answer "What a trouble! I have to keep my fingers moving on the phone the whole night, because I have so quit greeting message to send! "Yes, Merlin has such a long name list of his friends, and he wowould like to send a greeting message to each of them. what's worse, Merlin has another long name list of senders that have sent message to him, and he doesn' t want to send another message to bother them Merlin is so polite that he always replies each message he has es immediately ). so, before he begins to send message, he needs to fi Gure to how many friends are left to be sent. please write a program to help him. here is something that you shoshould note. first, Merlin's friend list is not ordered, and each name is alphabetic strings and case insensitive. these names are guaranteed to be not duplicated. second, some senders may send more than one message to Merlin, therefore the sender list may be duplicated. third, Merlin is kn Own by so many people, that's why some message senders are even not supported ded in his friend list. enter There are multiple test cases. in each case, at the first line there are two numbers n and m (1 <= n, m <= 20000 ), which is the number of friends and the number of messages he has encoded ed. and then there are n lines of alphabetic strings (the length of each will be less than 10), indicating the names of Me Rlin's friends, one per line. after that there are m lines of alphabetic strings, which are 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 left friends he must send. sample input 5 3InkfishHenryCarpMaxJerichoCarpMaxCarp0 sample output 3 dictionary tree .. [Cpp] # include <stdio. h> # include <string. h> # include <malloc. h> typedef struct NODE {short IsEnd; short IsShow; struct NODE * pNext [30];} NODE, * PNODE; void Init (PNODE * pRoot ); void Delete (PNODE pRoot);/* Delete tree */PNODE CreateNode ();/* create new node */void Insert (PNODE * pRoot, char * s ); /* Insert */int Search (PNODE * pRoot, char * s);/* Search */char ToLow (char c ); /* convert the input to a lowercase letter */int main () {PNODE pRoot = NULL; Init (& pRoot); char list [50]; int N, M; int n, m; int tot; while (scanf ("% d", & N )! = EOF & N) {n = N; scanf ("% d", & M); m = M; tot = 0; while (n --) {scanf ("% s", list); Insert (& pRoot, list) ;}while (m --) {scanf ("% s", list ); if (Search (& pRoot, list) tot ++;} printf ("% d \ n", N-tot); Delete (pRoot ); init (& pRoot);} return 0;} char ToLow (char c) {if (c> = 'A' & c <= 'Z') return c + 32; return c;} void Delete (PNODE pRoot) {int I; for (I = 0; I <30; ++ I) {if (pRoot-> pNext [I]! = NULL) Delete (pRoot-> pNext [I]);} free (pRoot);} int Search (PNODE * pRoot, char * s) {int I, k; PNODE p = * pRoot; if (! P) return 0; I = 0; while (s [I]) {k = ToLow (s [I ++])-'A'; if (! P-> pNext [k])/* No */return 0; p = p-> pNext [k];} if (1 = p-> IsEnd & 0 = p-> IsShow) {p-> IsShow = 1; return 1;} else return 0 ;} void Insert (PNODE * pRoot, char * s) {PNODE p = * pRoot; int I, k; if (! P) p = * pRoot = CreateNode (); I = 0; while (s [I]) {/* Find the corresponding branch */k = ToLow (s [I ++])-'A'; if (! P-> pNext [k])/* If this child is empty, create */{p-> pNext [k] = CreateNode ();} p = p-> pNext [k];} p-> IsEnd = 1;} PNODE CreateNode () {www.2cto.com PNODE p; int I; p = (PNODE) malloc (sizeof (NODE); p-> IsEnd = 0; p-> IsShow = 0; for (I = 0; I <30; ++ I) p-> pNext [I] = NULL; return p;} void Init (PNODE * pRoot) {(* pRoot) = NULL ;}

Related Article

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.