409 – Excuses, Excuses!

C++语言: Codee#25710001/

002+++++++++++++++++++++++++++++++++++++++

003author: chm

004+++++++++++++++++++++++++++++++++++++++

005
/

006

007#include



008#include

009#include

010#include

011#include

012#include

013#include

014#include

015#include

016#include

017#include

018#include

019#include

020#include

021#include

022#include

023#include

024

025usingnamespacestd;

026

027FILEfin=stdin;

028FILE
fout=stdout;

029constintmax_size=10086;

030

031charexcuses[99][99];

032charlower[99][99];

033charwords[99][99];

034

035typedefstructtag

036{

037intfrq;

038intnum;

039}Rec;

040Recrec[99];

041

042intwordcount(charsource,charstr)

043{

044charptr=strstr(source,str);

045inttms=0;

046intlen=strlen(str);

047

048while(ptr)

049{

050/


051abccde

052abc

053in this case,just check the tail

054/

055if(ptr==source&&!isalpha(
(ptr+len)))

056++tms;

057/

058abc
cde

059cde

060check the previous and next letter

061
/

062elseif(ptr!=source&&

063!isalpha((ptr-1))&&

064!isalpha(
(ptr+len)))

065++tms;

066ptr=strstr(ptr+1,str);

067}

068returntms;

069}

070

071intcmp(constvoida,constvoidb)

072{

073return((Rec)b)->frq-

074((Rec
)a)->frq;

075}

076

077intmain()

078{

079#ifndef ONLINE_JUDGE

080freopen("c:\in.txt","r",stdin);

081fout=fopen("c:\garage\out.txt","w");

082#endif

083intm,n;

084intcnt=1;

085

086while(scanf("%d%d\n",&m,&n)!=EOF)

087{

088for(inti=0;i<m;++i)

089scanf("%s\n",words[i]);

090memset(rec,0,sizeof(rec));

091

092for(inti=0;i<n;++i)

093{

094fgets(excuses[i],sizeof(excuses[i]),stdin);

095for(intj=0,len=strlen(excuses[i]);

096j<len;

097++j)

098lower[i][j]=tolower(excuses[i][j]);//save excuses in lower case

099rec[i].num=i;

100for(intj=0;j<m;++j)// for every word search in excuses

101rec[i].frq+=wordcount(lower[i],words[j]);

102}

103qsort(rec,sizeof(rec)/sizeof(rec[0]),sizeof(rec[0]),cmp);

104fprintf(fout,"Excuse Set #%d\n",cnt++);

105inttmp=rec[0].frq;

106for(inti=0;tmp==rec[i].frq;++i)

107fprintf(fout,"%s",excuses[rec[i].num]);

108fprintf(fout,"\n");

109}

110

111#ifndef ONLINE_JUDGE

112fclose(fout);

113system("c:\garage\check.exe");

114system("notepad c:\garage\out.txt");

115#endif

116return0;

117}原文链接: https://www.cnblogs.com/invisible/archive/2012/03/01/2376216.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/43026

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午7:43
下一篇 2023年2月8日 下午7:43

相关推荐