Nickname

来源:互联网 发布:淘宝大尺度买家秀18p 编辑:程序博客网 时间:2024/04/29 10:12
Problem 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<100000). 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 between two cases.
Sample Input
1
4
carp
inkfish
peipei
carp
Sample Output
carp 2
inkfish 1

peipei 1

//对快速排序算法的认识太过局限,快速排序不仅仅可以用于整数型,还可以用于其它数据类型

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>using namespace std;int n,t,cnt;char nd[100005][104],ans[100005][104];int num[100005];void quickSort(int left,int right){  int i=left; int j=right;char tmp[104];strcpy(tmp,nd[left]);if(left>=right)return;while(i!=j){while(i<j&&strcmp(nd[j],tmp)>0)j--;if(j>i)strcpy(nd[i++],nd[j]);while(i<j&&strcmp(nd[i],tmp)<0)i++;if(i<j)strcpy(nd[j--],nd[i]);}strcpy(nd[i],tmp);quickSort(left,i-1);quickSort(i+1,right);}int main(){//freopen("b.txt","r",stdin);scanf("%d",&t);char s[104];while(t--){scanf("%d",&n);int i,j,len;cnt=0;for(i=0;i<n;i++){scanf("%s",s);len=strlen(s);for(j=0;j<len;j++)if(s[j]>='A'&&s[j]<='Z')s[j]=s[j]-'A'+'a';strcpy(nd[i],s);}quickSort(0,n-1);cnt=0;num[cnt]=1;strcpy(ans[0],nd[0]);for(i=1;i<n;i++){//printf("%s\n",nd[i]);if(strcmp(ans[cnt],nd[i])==0)num[cnt]++;else {++cnt;strcpy(ans[cnt],nd[i]);num[cnt]=1;}}for(i=0;i<=cnt;i++)printf("%s %d\n",ans[i],num[i]);printf("\n");}return 0;}


0 0
原创粉丝点击