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.
题目:一组个数为N(<=100,000)的昵称,其中有大量的重复(总共昵称才不足5000个),要求求出每个昵称的个数。附加信息:昵称不分大小写,按照字典升序输出,昵称长度不超过100(记为L)。
解析:将昵称转换小写后,先快排,(O(Lnlongn)),再扫描一次重复的,(O(Ln))。
但这样虽然简单却复杂度较高。如果用hash,把字符串当作26进制数,求hash值(O(L)),用线性开放地址法解决hash冲突。这样做,复杂度复杂度为O(Ln)。
#include <stdio.h> #include <stdlib.h> #include <string.h> const int maxCount=6000; //hash表的最大槽数 typedef struct { char * name; int count; }Student; //比较大小的接口,用于qsort int cmp(const void* s1,const void* s2){ Student *ss1=(Student *)s1; Student *ss2=(Student *)s2; return strcmp(ss1->name,ss2->name); } //生成hash值 int hash(char* str){ int result=0; for(char* p=str;*p!=0;p++){ result=result*26+(*p)-96; } return result%maxCount; } //转换成小写 char *toLower(char *s){ char *p; for (p=s; *p; p++){ if (*p >= 'A' && *p <= 'Z') *p = *p - 'A' + 'a'; } return s; } Student hashTable[maxCount]; int main(){ FILE *input=fopen("nickname.in","r"); FILE *output=fopen("nickname.out","w"); int n,nickNum; //nickNum用来记录不重复昵称个数 int testcase,index; char str1[100],str2[100]; for( fscanf(input,"%d",&testcase);testcase>0;testcase--){ memset(hashTable,0,sizeof(hashTable)); fscanf(input,"%d",&n); //每一组测试的组数 for(int i=0;i<n;i++){ //n个昵称 fscanf(input,"%s",str1); strcpy(str2,toLower(str1)); index=hash(str2); //定位应该插入的位置 while(hashTable[index].name!=NULL&&strcmp(hashTable[index].name,str2)!=0){ //hash冲突 index++; if(index>=maxCount) index=0; } if(hashTable[index].name==NULL){ //第一次插入 hashTable[index].name=new char[strlen(str2)+1]; strcpy( hashTable[index].name,str2); hashTable[index].count=1; }else{ //非第一次插入 hashTable[index].count++; } } //将数据前移,然后排序输出 nickNum=0; for(int i=0;i<maxCount;i++){ if(hashTable[i].count!=0){ hashTable[nickNum++]=hashTable[i]; } } qsort(hashTable,nickNum,sizeof(Student),cmp); for(int i=0;i<nickNum;i++){ fprintf(output,"%s %d\n",hashTable[i].name,hashTable[i].count); } fprintf(output,"\n"); } return 0; }
输入:
3
3
hello
oh
oh
2
shit
shit
9
hello
put
abs
abs
put
hello
tte
hello
put
输出:
hello 1
oh 2
shit 2
abs 2
hello 3
put 3
tte 1
相关推荐
DB2创建NickName即联邦数据库SQL,供大家参考
客户端的实现效果 1.登录服务器,如果服务器端口号和IP号输入的字符都是"0"则,客户端连接到...检查用户名 :checkNickName(String nickName) 8.获得帮助列表:helpList(); 9.各项内容的输入在main方法中进行
int insertParam(@Param("nickName") String nickName, @Param("userCode") String userCode); @Insert("INSERT INTO USER(nick_name, user_code) VALUES(#{nickName,jdbcType=VARCHAR}, #{userCode,jdbcType=...
nickname changer to change nickname nickin
/nick <nickname> [player]设置您自己的昵称,可选的玩家参数来设置另一个玩家的昵称。 权限 nickname.use允许使用/nick 。 默认为所有用户。 nickname.color允许使用Nicks中的颜色。 默认为OP。 nickname.others...
nickname = "YOUR_NICKNAME" ; // Enter your nickname here im_admin = 1 ; // 1 == YES, 0 == NO 打开并打开正确的服务器 打开控制台(F12),然后在脚本上方按Enter键。 暂时不要关闭页面。 例子:
var PingPongNickname = require('pingpong-nickname'); var pingPongNickname = new PingPongNickname({host:'yourhost.com',port:3000,protocol:'http'},{auth:{user:'user',pass:'pass',...
Cause: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x8C\xB7' for column 'nickname' at row 1 解决方案 修改nickname的编码格式,没必要修改整个表。这种方式也不需要重启数据库,修改完即生效 ...
昵称和小名查找包含美国给定名称(名字)及其关联昵称或小名的CSV文件。 此查找文件最初是通过挖掘此创建的。 由于查找是基于用于家谱分析的数据集,因此有些旧名称最近很少使用,但也有最近使用的名称。...
Chinese-NickName-for-Sketch:用于草图的中文昵称生成插件
JMSN Auto Nickname Changer plugins support only winamp 2.x version. [StartUp] In Tools -> Options.. -> Auto Nickname tabs, Configure your prefix and postfix besides your mp3 name And Press ...
UNIQUE KEY `nickname_UNIQUE` (`nickname`) ); CREATE TABLE `tieba` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(100) NOT NULL, `content` varchar(600) NOT NULL, `name` varchar(18) NOT...
例如,下面的语句查询testtable表中姓名为“张三”的nickname字段和email字段。 SELECT nickname,email FROM testtable WHERE name='张三' (一) 选择列表 选择列表(select_list)指出所查询列,它可以是一组列名列表...
例如,下面的语句查询testtable 表中姓名为“张三”的nickname 字段和email 字段。 代码:SELECT `nickname`,`email`FROM `testtable`WHERE `name`='张三' (一) 选择列表 选择列表(select_list)指出所查询列,它可以...
userName: app.globalData.userInfo.nickName, password: '', useSSL: false, cleanSession: false, keepAliveInterval: 60, onSuccess: function () { app.globalData.client.subscribe(that.data.channel, ...
结果示例图 (此图片来源于网络,如有侵权,请联系删除! ) conversation.png 思路 善用flex 对row左右对开 东边上面对开 东北角左右分散对齐 ... class="nickname">{{item.nickname}} class="datetime">{{it
CREATE TABLE `random_nickname` ( `id` bigint NOT NULL AUTO_INCREMENT, `nickname` varchar(100) NOT NULL COMMENT '昵称', `type` varchar(20) DEFAULT NULL COMMENT '类型', `tag` varchar(10) DEFAULT '' ...
例如,下面的语句查询testtable表中姓名为“张三”的nickname字段和email字段。 代码:SELECT `nickname`,`email`FROM `testtable`WHERE `name`=’张三’ (一) 选择列表 选择列表(select_list)指出所查询列,它可以是...
//nickname = outputObj1["nickname"].ToString(); //昵称 //sex = outputObj1["sex"].ToString(); //性别 headimgurl = outputObj1["headimgurl"].ToString(); //头像url //province = outputObj1["province"]....
# 分析网页的结构,找到数据所在的标签位置: video-info { video-nickname, video-number } # 待分析网页数据 # # 春季赛RW vs EDG">LPL春季赛RW vs EDG # <span class="video-nickname" title="LPL熊猫官方...