A 谍报分析 河南第十届ACM真题 【字符串处理】

/*
统计一篇单词,先按照数量多少排序,再按照字符串字典序排序。

*/

字符串每次做都是各种细节,好废时间。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node{
	char str[25];
	int cnt;
}edge[10005];
int cmp(Node a,Node b){
	if(a.cnt!=b.cnt){
		return a.cnt > b.cnt;//降序排 
	}else
	{
		int x = strcmp(a.str,b.str);
		if(x==-1){
			return 1;
		}else{
			return 0;
		}
	}
}
int main()
{
	char str[25];
	int k = 0,i,j,flag;
	edge[0].cnt = 1;
	while(scanf("%s",str)!=EOF){
		flag = 0;
		if(str[0]==',' || str[0]=='.'){
			continue;
		}
		int len = strlen(str);
		if(str[len-1]==',' || str[len-1]=='.'){//有, .处理一下。 
			str[len-1] = '\0';
		}
		if(k==0){  //第一个直接放进去 
			strcpy(edge[k++].str,str);
		}
		else{
			for(i=0;i<k;i++){
				if(strcmp(edge[i].str,str)==0){//如果有相同的不放 数量++ 
					edge[i].cnt++;
					flag = 1;
				}
			}
			if(flag == 0){//没有相同的 继续放,初始值为 1 
				strcpy(edge[k].str,str);
				edge[k++].cnt = 1;
			}
		}
	}
	sort(edge,edge+k,cmp);
	int res = 10;
	if (k<10) {
		res = k;
	}
	for(i=0;i<res;i++){
		printf("%s %d\n",edge[i].str,edge[i].cnt);
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值