UVa 10815 - Andy's First Dictionary

看到这题时我就一直在想..如何比较单词是否一样呢...存到一个数组里?然后blabla...

简直No Law To See.

然后我就去看解题报告了...

然后我才发现原来qsort也能排二维数组的.那这样问题就解决了,开一个很大的二维数组,然后一个单词一个单词存进去.qsort一下,如果不同的字符串就输出.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char word[100100][210] = {'\0'};
int cmp_string(const void *_a, const void *_b)
{
    char *a = (char *) _a;
    char *b = (char *) _b;
    return strcmp(a, b);
}
int main()
{
    //freopen("input.txt","r",stdin);
    int i = 0, j = 0;
    char ch;
    while (1)
    {
        if ((ch = getchar()) == EOF)
            break;
        if (isalpha(ch))
        {
            word[i][j++] = tolower(ch);
        }
        else if (isalpha(word[i][0]))
        {
             word[i++][j] = '\0';
             j = 0;
        }
 
    }
    qsort(word,i,sizeof(word[0]),cmp_string);
    for (j = 0; j < i; j++)
        if (strcmp(word[j], word[j + 1]))
            printf("%s\n",word[j]);
    return 0;
}


你可能感兴趣的:(ACM,uva,10815)