现在的位置: 首页 > 综合 > 正文

UVA10815 Andy’s First Dictionary

2013年10月12日 ⁄ 综合 ⁄ 共 647字 ⁄ 字号 评论关闭

        排序 + 分离单词技巧

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

bool isLetter(char &ch) {
    if (ch >= 'a' && ch <= 'z')
        return true;
    if (ch >= 'A' && ch <='Z') {
        ch += 'a' - 'A';
        return true;
    }
    return false;
}

int cmp(const void* _a, const void* _b) {
    char *a = (char*)_a;
    char *b = (char*)_b;
    return strcmp(a, b);
}

char b[50005][205];
int main()
{
    char tmp;

    bool flag = false;
    int i = 0, j = 0;

    while ((tmp = getchar()) != EOF) {

        if (isLetter(tmp) && !flag) {
            i = 0;
            b[j][i++] = tmp;
            flag = true;
        }
        else if (isLetter(tmp) && flag) {
            b[j][i++] = tmp;
        }
        else if (!isLetter(tmp) && flag) {
            flag = false;
            b[j][i] = '\0';
            j++;
        }
    }

    qsort(b, j, sizeof (b[0]), cmp);

    for (i=0; i<j-1; i++)
    {
        if (0 == strcmp(b[i], b[i+1]))
            continue;
        printf("%s\n", b[i]);
    }
    printf("%s\n", b[i]);
    return 0;
}

 

 

抱歉!评论已关闭.