409 - Excuses, Excuses!

C++语言: Codee#25710
001 /*
002 +++++++++++++++++++++++++++++++++++++++
003                 author: chm
004 +++++++++++++++++++++++++++++++++++++++
005 */
006
007 #include <map>
008 #include <set>
009 #include <list>
010 #include <queue>
011 #include <cmath>
012 #include <stack>
013 #include <bitset>
014 #include <cstdio>
015 #include <cctype>
016 #include <vector>
017 #include <cstdlib>
018 #include <cstring>
019 #include <fstream>
020 #include <sstream>
021 #include <iomanip>
022 #include <iostream>
023 #include <algorithm>
024
025 using namespace std;
026
027 FILE*            fin         = stdin;
028 FILE*            fout         = stdout;
029 const int        max_size     = 10086;
030
031 char excuses[99][99];
032 char lower[99][99];
033 char words[99][99];
034
035 typedef struct tag
036 {
037     int frq;
038     int num;
039 } Rec;
040 Rec rec[99];
041
042 int    wordcount(char* source, char* str)
043 {
044     char* ptr = strstr(source, str);
045     int tms = 0;
046     int len = strlen(str);
047
048     while(ptr)
049     {
050         /*
051         abc*cde*
052         abc
053         in this case,just check the tail
054             */
055         if(ptr == source && !isalpha(*(ptr + len)))
056             ++tms;
057         /*
058         abc*cde*
059             cde
060         check the previous and next letter
061             */
062         else if(ptr != source &&
063                 !isalpha(*(ptr - 1)) &&
064                 !isalpha(*(ptr + len)))
065             ++tms;
066         ptr = strstr(ptr + 1, str);
067     }
068     return    tms;
069 }
070
071 int cmp(const void* a, const void* b)
072 {
073     return ((Rec *)b)->frq -
074            ((Rec *)a)->frq;
075 }
076
077 int main()
078 {
079 #ifndef ONLINE_JUDGE
080     freopen("c:\\in.txt", "r", stdin);
081     fout = fopen("c:\\garage\\out.txt", "w");
082 #endif
083     int m, n;
084     int cnt = 1;
085
086     while(scanf("%d%d\n", &m, &n) != EOF)
087     {
088         for(int i = 0; i < m; ++i)
089             scanf("%s\n", words[i]);
090         memset(rec, 0, sizeof(rec));
091
092         for(int i = 0; i < n; ++i)
093         {
094             fgets(excuses[i], sizeof(excuses[i]), stdin);
095             for(int j = 0, len = strlen(excuses[i]);
096                     j < len;
097                     ++j)
098                 lower[i][j] = tolower(excuses[i][j]);     //save excuses in lower case
099             rec[i].num = i;
100             for(int j = 0; j < m; ++j)                    // for every word search in excuses
101                 rec[i].frq += wordcount(lower[i], words[j]);
102         }
103         qsort(rec, sizeof(rec) / sizeof(rec[0]), sizeof(rec[0]), cmp);
104         fprintf(fout, "Excuse Set #%d\n", cnt++);
105         int tmp = rec[0].frq;
106         for(int i = 0; tmp == rec[i].frq; ++i)
107             fprintf(fout, "%s", excuses[rec[i].num]);
108         fprintf(fout, "\n");
109     }
110
111 #ifndef ONLINE_JUDGE
112     fclose(fout);
113     system("c:\\garage\\check.exe");
114     system("notepad c:\\garage\\out.txt");
115 #endif
116     return 0;
117 }
posted @ 2012-03-01 21:06  strorehouse  阅读(272)  评论(0编辑  收藏  举报