HDU计算机学院大学生程序设计竞赛(2015’12)1008 Study Words

题意:

输入一篇文章,输出出现最多的10个单词

不包括old里的已有单词

出现次数相同按字典序大小

 

简单模拟,map记录个数

没有什么坑点

然后sort一下

用string 可以直接比较字典序大小

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<map>
#include<math.h>
using namespace std;
char s[205],m[]={"</article>"};
struct node
{
    string a;
    int v;
}p[150005];
bool cmp(node a,node b)
{
    if(a.v==b.v) return a.a<b.a;
    return a.v>b.v;
}
int main()
{
    int i,j,n;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        map<string,int> f;
        while(scanf("%s",s))
        {
            int l=strlen(s);
            for(i=0;i<l;i++)
            {
                if(s[i]>='A'&&s[i]<='Z') s[i]+=32;
            }
            if(s[0]=='<') break;f[s]=-1;
        }
        scanf("%s",s);
        int cnt=0;
        while(scanf("%s",s))
        {
            int l=strlen(s);
            if(l==10)
            {
                for(i=0;i<l;i++)
                {
                    if(s[i]==m[i]) continue;
                    break;
                }
                if(i==10) break;
            }
            string tp="";
            for(i=0;i<l;i++)
            {
                if(s[i]>='A'&&s[i]<='Z') s[i]+=32;
                if(s[i]>='a'&&s[i]<='z')
                    tp+=s[i];
                else
                {
                    if(tp=="") continue;
                    if(f[tp]==0)
                    {
                        f[tp]=++cnt;
                        p[cnt].a=tp;
                        p[cnt].v=1;
                    }
                    else
                        p[f[tp]].v++;
                    tp="";
                }
            }
            if(tp=="") continue;
                    if(f[tp]==0)
                    {
                        f[tp]=++cnt;
                        p[cnt].a=tp;
                        p[cnt].v=1;
                    }
                    else
                        p[f[tp]].v++;
        }
        sort(p+1,p+1+cnt,cmp);
        for(i=1;i<=10&&i<=cnt;i++) cout<<p[i].a<<endl;
        cout<<endl;
    }
    return 0;
}

 

  

转载于:https://www.cnblogs.com/Woo95/p/5078318.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值