sicily 1194. Message Flood

Description

Well, how do you feel about mobile phone? Your answer would 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 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 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 receives immediately). So, before he begins to send messages, he needs to figure to how many friends are left to be sent. Please write a program to help him.

Here is something that you should 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 known by so many people, that’s why some message senders are even not included in his friend list.


Input

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 received. And then there are n lines of alphabetic strings (the length of each will be less than 10), indicating the names of Merlin’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.

暴力搜索肯定是不能滴……只要快排+二分就能过了

同样不给用qsort,又要自己写快排= = 歧视写C的嘛!为啥sort就行qsort就不给用……

貌似用C++的话可以用各种容器搞定,等学了再回头来看看

AC runtime 0.06s

View Code
  1 #include <stdio.h>
  2 #include <string.h>
  3 char fri[20000][11];
  4 char sent[20000][11];
  5 
  6 void strUpr( char *str )
  7 {
  8     int i, len = strlen(str);
  9     
 10     for ( i = 0; i < len; i++ )
 11     {
 12         if( str[i] <= 'z' && str[i] >= 'a' )
 13             str[i] = str[i] - 'a' + 'A';
 14     }
 15 }
 16 
 17 int biSrch( int m, char str[] )
 18 {
 19     int start = 0, end = m - 1;
 20     int mid;
 21     
 22     while (start <= end)
 23     {
 24         mid = ( start + end ) / 2;
 25  
 26         if ( strcmp( str, sent[mid] ) == 0 )
 27             return 1;
 28         else if ( strcmp( str, sent[mid] ) == -1 )
 29             end = mid - 1;
 30         else
 31             start = mid + 1;
 32     }
 33     
 34     return 0;
 35 }
 36 
 37 void quick( int l, int r )
 38 {
 39     int i, j;
 40     char x[11];
 41     
 42     if ( l >= r )
 43         return;
 44     
 45     i = l;
 46     j = r;
 47     strcpy( x, sent[i] );
 48     
 49     while( i < j )
 50     {
 51         while ( i < j && strcmp( sent[j], x ) == 1 )
 52             j--;
 53         if ( i < j )
 54             strcpy( sent[i++], sent[j] );
 55         
 56         while ( i < j && strcmp( sent[i], x ) == -1 )
 57             i++;
 58         if ( i < j )
 59             strcpy( sent[j--], sent[i] );
 60     }
 61     
 62     strcpy( sent[i], x );
 63     quick( l, i - 1);
 64     quick( i + 1, r);
 65 }
 66 
 67 
 68 int main()
 69 {
 70     int i, m, n, count;
 71     
 72     while( scanf("%d", &n) && n != 0 )
 73     {
 74         scanf("%d", &m);
 75         count = 0;
 76         for( i = 0; i < n; i++ )
 77         {
 78             scanf("%s", fri[i]);
 79             strUpr(fri[i]);
 80         }
 81             
 82         
 83         for ( i = 0; i < m; i++ )
 84         {
 85             scanf("%s", sent[i]);
 86             strUpr(sent[i]);
 87         }
 88         
 89         quick( 0, m - 1 );
 90         
 91         for ( i = 0; i < n; i++ )
 92         {
 93             if ( biSrch ( m, fri[i] ) )
 94                 count++;
 95         }
 96         
 97         printf("%d\n", n - count);
 98     }
 99 
100     return 0;
101 }
原文地址:https://www.cnblogs.com/joyeecheung/p/2877490.html