POJ1598-C语言

来源:互联网 发布:mac可以玩lucky dog1 编辑:程序博客网 时间:2024/05/17 22:05

题目:

Excuses, Excuses!
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 4319 Accepted: 1483

Description

Judge Ito is having a problem with people subpoenaed for jury duty giving rather lame excuses in order to avoid serving. In order to reduce the amount of time required listening to goofy excuses, Judge Ito has asked that you write a program that will search for a list of keywords in a list of excuses identifying lame excuses. Keywords can be matched in an excuse regardless of case.

Input

Input to your program will consist of multiple sets of data. Line 1 of each set will contain exactly two integers. The first number (1 <= K <= 20) defines the number of keywords to be used in the search. The second number (1 <= E <= 20) defines the number of excuses in the set to be searched. Lines 2 through K+1 each contain exactly one keyword. Lines K+2 through K+1+E each contain exactly one excuse. All keywords in the keyword list will contain only contiguous lower case alphabetic characters of length L (1 <= L <= 20) and will occupy columns 1 through L in the input line. All excuses can contain any upper or lower case alphanumeric character, a space, or any of the following punctuation marks [".,!?] not including the square brackets and will not exceed 70 characters in length. Excuses will contain at least 1 non-space character.

Output

For each input set, you are to print the worst excuse(s) from the list. The worst excuse(s) is/are defined as the excuse(s) which contains the largest number of incidences of keywords. If a keyword occurs more than once in an excuse, each occurrance is considered a separate incidence. A keyword "occurs" in an excuse if and only if it exists in the string in contiguous form and is delimited by the beginning or end of the line or any non-alphabetic character or a space. 

For each set of input, you are to print a single line with the number of the set immediately after the string "Excuse Set #". (See the Sample Output). The following line(s) is/are to contain the worst excuse(s) one per line exactly as read in. If there is more than one worst excuse, you may print them in any order. After each set of output, you should print a blank line. 

Sample Input

5 3dogatehomeworkcanarydiedMy dog ate my homework.Can you believe my dog died after eating my canary... AND MY HOMEWORK?This excuse is so good that it contain 0 keywords.6 5superhighwaycrazythermonuclearbedroomwarbuildingI am having a superhighway built in my bedroom.I am actually crazy.1234567890.....,,,,,0987654321?????!!!!!!There was a thermonuclear war!I ate my dog, my canary, and my homework ... note outdated keywords?

Sample Output

Excuse Set #1Can you believe my dog died after eating my canary... AND MY HOMEWORK?Excuse Set #2I am having a superhighway built in my bedroom.There was a thermonuclear war!
个人理解:

字符串匹配,不过需要注意的就是大小写忽略

而且子字符串必须在原字符串中是完整的字母

函数链接:

单词匹配函数

直接返回匹配是否成功:

AC情况:


代码:C语言

# include <stdio.h># define check(t)  (t<'a'||t>'z')&&(t<'A'||t>'Z')# define N 70# define M 20# define MAX(a,b) (a)>(b)?(a):(b)int CMP(char a,char b){   return  a==b||b==a+32||a==b+32;}int BF(char a[],char b[]){//a中如果有b这个单词返回1 否则返回0    int i=0,j=0;    do{        if (b[j]&&CMP(a[i++],b[j]))++j;//若a[i]与b[j]相等或是大小写关系 继续比较        else{           if(!b[j])           {//如果b字符串到头了 此时需要检查 b在a字符串是否为完整的单词           if((i!=j)&&check(a[i-j-1])||(i==j))//i==j时 b在a字符串的串首  如果不在串首,则i!=j 此时应检查a[i-j-1]           if(!a[i]||a[i]&&check(a[i]))//如果b在a字符串的串尾 则a[i]==0  如果不在末尾 则检验 a[i]                return 1;//此时b在a里是个完整的单词 返回1           }           else i-=j;            j=0;        }    }while(a[i-1]);        return 0;//检查完没有返回0}char A[M][M+1],B[M][N+1];int n,m,s[20],i,j,max,t;int main(){     //freopen("AAA.txt","r",stdin);     while(scanf("%d %d ",&n,&m)!=EOF){         for(i=0;i<n;i++)            gets(A[i]);//主串         for(i=0,max=0;i<m;i++)         {             s[i]=0;             gets(B[i]);//次串             for(j=0;j<n;j++)                 s[i]+=BF(B[i],A[j]);//记录句子中的符合单词                 max=MAX(max,s[i]);//记录最大值         }         printf("Excuse Set #%d\n",++t);         for(i=0;i<m;i++)           if(s[i]==max)puts(B[i]);//和最大值相同的都输出         printf("\n");     }return 0;}

________________________________________

# include <stdio.h># include <string.h># define At(t)   (t<'a'||t>'z')# define N 70# define M 20# define MAX(a,b) (a)>(b)?(a):(b)# define CMP(a,b)  ((a)==(b)+32||(b)==(a)+32)char A[M][M+1],B[M][N+1];int n,m,s[20],i,j,max,t=0;int BF(char a[],char b[]){    int i=0,j=0;    do{        if (b[j]&&(a[i++]==b[j]||CMP(a[i-1],b[j])))++j;        else{           if(!b[j]){            if((i!=j)&&At(a[i-j-1])||(i==j))if(!a[i]||a[i]&&At(a[i]))            return 1;           }           else i-=j;            j=0;        }    }while(a[i-1]);        return 0;} int main(){     while(scanf("%d %d ",&n,&m)!=EOF){         for(i=0;i<n;i++)            gets(A[i]);//主串         for(i=0,max=0;i<m;i++)         {             s[i]=0;             gets(B[i]);//次串             for(j=0;j<n;j++)                 s[i]+=BF(B[i],A[j]);//记录句子中的符合单词                 max=MAX(max,s[i]);//记录最大值         }         printf("Excuse Set #%d\n",++t);         for(i=0;i<m;i++)           if(s[i]==max)              puts(B[i]);//和最大值相同的都输出     printf("\n");     }return 0;}
解释: