10815 Andy's First Dictionary

/*

这段代码写得奇丑无比

好像读了空串进来

前几次提交都是RE

有空再改

*/

C++语言: Codee#25713
001 /*
002 +++++++++++++++++++++++++++++++++++++++
003                 author: chm
004 +++++++++++++++++++++++++++++++++++++++
005 */
006 /*
007    what an ugly code
008    */
009 #include <map>
010 #include <set>
011 #include <list>
012 #include <queue>
013 #include <cmath>
014 #include <stack>
015 #include <bitset>
016 #include <cstdio>
017 #include <cctype>
018 #include <vector>
019 #include <cstdlib>
020 #include <cstring>
021 #include <fstream>
022 #include <sstream>
023 #include <iomanip>
024 #include <iostream>
025 #include <algorithm>
026
027 using namespace std;
028
029 FILE*            fin         = stdin;
030 FILE*            fout         = stdout;
031 const int        max_size     = 50086;
032
033 char words[max_size][201];
034
035 int cmp(const void* a, const void* b)
036 {
037     return strcmp(((char *)a),
038                   ((char *)b));
039 }
040
041 int main()
042 {
043 #ifndef ONLINE_JUDGE
044     freopen("c:\\in.txt", "r", stdin);
045     fout = fopen("c:\\garage\\out.txt", "w");
046 #endif
047     char tmp;
048     int in = 0;
049     int i = 0, j = 0;
050
051
052     while((tmp = getchar()) != EOF)
053     {
054         if(isalpha(tmp))
055         {
056             if(!in)
057                 in = 1;
058             words[i][j++] = tolower(tmp);
059         }
060         else if(!isalpha(tmp) && in)
061         {
062             in = 0;
063             words[i][j] = '\0';
064             ++i;
065             j = 0;
066         }
067     }
068
069     /*
070        |word another word(EOF)
071            |
072         */
073     /*
074     while((tmp = getchar()) != EOF)
075     {
076     if(isalpha(tmp) && !in)            //the first time get into a word
077     {
078         j = 0;
079         words[i][j++] = tolower(tmp);
080         in = 1;
081     }
082     else if(isalpha(tmp) && in)     //already in a word
083         words[i][j++] = tolower(tmp);
084     else if(!isalpha(tmp) && in)    //get out of a word
085     {
086         in = 0;
087         words[i][j] = '\0';
088         ++i;
089     }
090     }
091     */
092
093     qsort(words, i, sizeof(words[0]), cmp);
094     int k;
095     for(k = 0; !isalpha(words[k][0]); ++k)
096         ;
097     char* pre = words[k];
098     fprintf(fout, "%s\n", pre);
099     for(++k; k < i; ++k)
100         if(strcmp(pre, words[k]))
101         {
102             fprintf(fout, "%s\n", words[k]);
103             pre = words[k];
104         }
105
106 #ifndef ONLINE_JUDGE
107     fclose(fout);
108     system("c:\\garage\\check.exe");
109     system("notepad c:\\garage\\out.txt");
110 #endif
111     return 0;
112 }
原文地址:https://www.cnblogs.com/invisible/p/2377358.html