2015.12杭电校赛 1008 Study Words

经典的字符串查找

Study Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 226    Accepted Submission(s): 80


Problem Description
Learning English is not easy, vocabulary troubles me a lot.
One day an idea came up to me: I download an article every day, choose the 10 most popular new words to study.
A word's popularity is calculated by the number of its occurrences.
Sometimes two or more words have the same occurrences, and then the word with a smaller lexicographic has a higher popularity.
 

Input
T in the first line is case number.
Each case has two parts.
<oldwords>
...
</oldwords>
<article>
...
</article>
Between <oldwords> and </oldwords> are some old words (no more than 10000) I have already learned, that is, I don't need to learn them any more.
Words between <oldwords> and </oldwords> contain letters ('a'~'z','A'~'Z') only, separated by blank characters (' ','\n' or '\t').
Between <article> and </article> is an article (contains fewer than 1000000 characters).
Only continuous letters ('a'~'z','A'~'Z') make up a word. Thus words like "don't" are regarded as two words "don" and "t”, that's OK.
Treat the uppercase as lowercase, so "Thanks" equals to "thanks". No words will be longer than 100.
As the article is downloaded from the internet, it may contain some Chinese words, which I don't need to study.
 

Output
For each case, output the top 10 new words I should study, one in a line.
If there are fewer than 10 new words, output all of them.
Output a blank line after each case.
 

Sample Input
  
  
2 <oldwords> how aRe you </oldwords> <article> --How old are you? --Twenty. </article> <oldwords> google cn huluobo net i </oldwords> <article> 文章内容: I love google,dropbox,firefox very much. Everyday I open my computer , open firefox , and enjoy surfing on the inter- net. But these days it's strange that searching "huluobo" is unavail- able. What's wrong with "huluobo"? </article>
 

Sample Output
  
  
old twenty firefox open s able and but computer days dropbox enjoy
#include <cstdio>
#include <cctype>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#define MS(x,y) memset(x,y,sizeof(x))
void fre(){freopen("t.txt","r",stdin);}
using namespace std;

char s[205],word[101];
set<string>ow;
map<string,int>pw;
bool cmp(pair<string,int> a,pair<string,int> b)
{
    if(a.second == b.second) return a.first < b.first;
    else return a.second > b.second;
}
int main()
{
   // fre();
    int t,i,j;
    char *st,*ed;
    scanf("%d",&t);
    while(t--)
    {
        pw.clear();
        ow.clear();
        scanf("%s",s);
        while(scanf("%s",s),strcmp(s,"</oldwords>"))
        {
            for(i = 0; s[i]; ++i) s[i]=tolower(s[i]);
            ow.insert(s);
        }
        scanf("%s",s);
        while(scanf("%s",s),strcmp(s,"</article>"))
        {
            for(i = 0; s[i]; ++i) if(s[i]>='A'&&s[i]<='Z')s[i]=tolower(s[i]);
            i = 0;
            st = s;
            while(*st)
            {
                while((*st<'a'||*st>'z')&&*st)
                {
                    st++;
                }
                while(*st>='a'&&*st<='z')
                {
                    word[i++]=*st;
                    st++;
                }
                if(i==0)continue;
                word[i]=0;
                if(!ow.count(word)) pw[word]++;
                i=0;
            }
        }
        vector<pair<string,int> >vct(pw.begin(),pw.end());
        sort(vct.begin(),vct.end(),cmp);
        for(vector<pair<string,int> >::iterator it = vct.begin(); it < vct.end(); it++)
        {
            printf("%s\n",it->first.c_str());
            if(it - vct.begin() ==9) break;
        }
        puts("");
    }

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值