UVA409

唉,渣就是渣,掩饰不了,每次都把找BUG时加的额外语句提交时忘记注释掉,导致错误,

本来是一次AC的,另外贴上我自己的龊代码:

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int K,E;
char key[21][21],exc[21][72],excca[21][72];
int excc[21];
int aa=0;
while(scanf("%d%d",&K,&E)!=EOF)
{
getchar();
for(int i=0;i<K;i++)
gets(key[i]);
for(int i=0;i<E;i++)
gets(excca[i]);
for(int i=0;i<E;i++)
for(int j=0;excca[i][j]!='\0';j++)
{
if(excca[i][j]>='A'&&excca[i][j]<='Z')
exc[i][j]=excca[i][j]-'A'+'a';
else
exc[i][j]=excca[i][j];
}
int max=0;
for(int i=0;i<E;i++)
{
int sum=0;
for(int j=0;j<K;j++)
{
int len=strlen(key[j]);
for(int k=0;exc[i][k]!='\0';k++)
{
if((key[j][0]==exc[i][k])&&(k==0||exc[i][k-1]==' '||exc[i][k-1]<'a'||exc[i][k-1]>'z')&&
(exc[i][k+len]=='\0'||exc[i][k+len]==' '||exc[i][k+len]<'a'||exc[i][k+len]>'z'))
{
int f=0;
for(f=0;f<len;f++)
if(key[j][f]!=exc[i][k+f]) break;
if(f==len)
{
sum++;
k+=len-1;
}
}
}
}
excc[i]=sum;
if(sum>max)
max=sum;
}
// printf("%d\n",max);
printf("Excuse Set #%d\n",++aa);
for(int i=0;i<E;i++)
if(excc[i]==max)
puts(excca[i]);
puts("");
}
}

另外这题也可以通过取字串来做相对简洁点:

 #include<stdio.h>
 2 #include<string.h>
 3 #include<ctype.h>
 4 char key[25][25];
 5 char exc[25][100];
 6 int cnt[25];
 7 int k,e;
 8 int keycnt(char *str)
 9 {
10     int i,j=0,number=0;
11     char s[100];
12     for(i=0;str[i]!='\0';i++)
13     {
14         if(isalpha(str[i]))
15         {
16             s[j]=tolower(str[i]);
17             j++;
18         }
19         else
20         {
21             s[j]='\0';
22             for(j=0;j<k;j++)
23             {
24                 if(strcmp(s,key[j])==0)
25                     number++;
26             }
27             j=0;
28         }
29     }
30     return number;
31 }
32 int main()
33 {
34     int now=0;
35     while(scanf("%d%d",&k,&e)==2)
36     {
37         getchar();
38         memset(cnt,0,sizeof(cnt));
39         int i,j,r;
40         for(i=0;i<k;i++)
41         {
42             gets(key[i]);
43         }
44         for(i=0;i<e;i++)
45         {
46             gets(exc[i]);
47         }
48         for(i=0;i<e;i++)
49         {
50             cnt[i]=keycnt(exc[i]);
51         }
52         int max=0;
53         for(i=0;i<e;i++)
54             max=(max>cnt[i])?max:cnt[i];
55         printf("Excuse Set #%d\n",++now);
56         for(i=0;i<e;i++)
57         {
58             if(cnt[i]==max)
59             {
60                 puts(exc[i]);
61             }
62         }
63         printf("\n");
64     }
65     return 0;
66 }
另外此题还可以用strstr来做:

包含文件: string.h
函数名: strstr
函数原型:extern char *strstr(char *str1, const char *str2);
语法:* strstr(str1,str2)
str1: 被查找目标 string expression to search.
str2: 要查找对象 The string expression to find.
返回值:若str2是str1的子串,则先确定str2在str1的第一次出现的位置,并返回此位置到str1末尾的所有字符;如果str2不是str1的子串,则返回NULL。(注:若想返回str2在str1第一次出现的位置,不是这个函数)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值