| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
痛苦的wa!!!哪位高人能帮帮忙 同学们提到的注意的问题都注意到了,自己也试了n组数据都没问题,可还是wa
#include<iostream>
using namespace std;
bool isalphet(char);
int has(char *,char *);
int main()
{
int m,n,i,j,max;
char key[25][30];
char execuse[25][80];
int num[25];
char ch;
int count=1;
while(cin>>m>>n)
{
max=0;
for(i=0;i<m;i++)
{
cin>>key[i];//输入关键字
}
ch=getchar();
for(i=0;i<n;i++)
{
gets(execuse[i]);num[i]=0;//输入句子
for(j=0;j<m;j++)
{
num[i]+=has(key[j],execuse[i]);//计算每句话中含key的个数
}
if(num[i]>max) max=num[i];
}
cout<<"Execuse Set #"<<count<<endl;
for(i=0;i<n;i++)
{
if(num[i]==max) cout<<execuse[i]<<endl;
}
count++;
}
system("pause");
return 0;
}
//返回e2中匹配单词k2的个数
int has(char *k2,char *e2)
{
int i,j,k,e,f;
char ch;
int num=0;
k=0;f=0;while(!isalphet(e2[f])){f++;}//找到e2中是字母开始出现的位置
e=f;//k是k2的指针,f是e2大循环的指针,e是e2小循环的指针
while(f<=strlen(e2)-strlen(k2))
{
ch=tolower(e2[e]);
if(k2[k]==ch){ k++;e++;}//相同则继续比较
else{ k=0;//不同时k返回k2的起点准备重新比较
f++;
while(f!=0&&isalphet(e2[f])){ f++;}//跨过该字母串(因为不匹配),找到非字母的位置
while(!isalphet(e2[f])&&f<=strlen(e2)-strlen(k2)){f++;}//找到下一组比较开始的字母的位置
e=f;
continue;}
if(k==strlen(k2)){
if(!isalphet(e2[e])){//比较结束匹配并且下一个字符不是字母,保证是一个单词,而不是单词的子串
num++;}k=0;
f++;
while(f!=0&&isalphet(e2[f])){ f++;}
while(!isalphet(e2[f])){f++;}
e=f;
}
}
return num;
}
//判断字符是否为字母
bool isalphet(char c)
{
int temp;
temp=c-'a';
if(temp>=0&&temp<=26) return true;
temp=c-'A';
if(temp>=0&&temp<=26) return true;
return false;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator