Description
ZSUQ Messenger is similar with Tencent QQ. Each user will make a nickname for itself. Different users can have identical nickname. Some common ones, such as “Tom”, “Marry”, “Kate”, are frequently used. In a recent survey, the ZSUQ Company was astonished to find that there are no more than 5000 distinct nicknames that are being used.
Being a member of the ZSUQ Company, you are asked to make a report, telling the number of users for each nick name. A complete list of all users’ nicknames is provided to you. Please note that nicknames are case insensitive.
Input
The first line of the file contains an integer indicating the number of test cases.
In each test case, there is an integer N in the first line (0 < N < 100,000). The following N lines describe N users’ nicknames. Each name consists of no more than 100 characters. There is a bland line between two cases.
Output
For each case, give out a list of the distinct nickname, one per line, followed by a bland space and the number of users who has this nickname. The names are in alphabetic order. There is no bland space at the beginning or the end of each line. Nicknames should be presented in lowercase letter. There is a bland line after each case.
水题,转小写预处理,快排,新开字符串数组去重,开一个数组计数,就行了
run time 0.62sec
1 #include<stdio.h> 2 #include<string.h> 3 char nn[100000][101]; 4 char fltrd[100000][101]; 5 int count[100000]; 6 void quicksort( int l, int r ); 7 void lower( char str[] ); 8 9 int main() 10 { 11 int t, n, i, j; 12 scanf("%d", &t); 13 14 while(t--) 15 { 16 scanf("%d", &n); 17 18 for ( i = 0; i < n; i++ ) 19 { 20 scanf("%s", nn[i]); 21 lower( nn[i] ); 22 } 23 24 quicksort( 0, n-1 ); 25 26 strcpy( fltrd[0], nn[0] ); 27 28 j = 0; 29 30 for( i = 0; i < n; i++ ) 31 count[i] = 1; 32 33 for ( i = 1; i < n; i++ ) 34 { 35 if( strcmp(fltrd[j],nn[i]) != 0 ) 36 strcpy( fltrd[++j], nn[i] ); 37 else 38 count[j]++; 39 } 40 41 for ( i = 0; i <= j; i++ ) 42 printf("%s %d\n", fltrd[i], count[i] ); 43 44 printf("\n"); 45 } 46 47 return 0; 48 } 49 50 void quicksort( int l, int r ) 51 { 52 int i = l, j = r; 53 char x[101]; 54 55 if ( l >= r ) 56 return; 57 58 strcpy( x, nn[i] ); 59 60 while ( j > i ) 61 { 62 while( j > i && strcmp(nn[j], x) == 1 ) 63 j--; 64 if( j > i ) 65 strcpy( nn[i++], nn[j] ); 66 67 while( j > i && strcmp(nn[i], x) == -1 ) 68 i++; 69 if( j > i ) 70 strcpy( nn[j--], nn[i] ); 71 } 72 73 strcpy( nn[i], x ); 74 quicksort( l, i-1 ); 75 quicksort( i+1, r ); 76 } 77 78 void lower( char str[] ) 79 { 80 int i; 81 82 for ( i = 0; str[i] != '\0'; i++ ) 83 if ( str[i] >= 'A' && str[i] <= 'Z') 84 str[i] = str[i] - 'A' + 'a'; 85 }