zoukankan      html  css  js  c++  java
  • UVA10815 Andy's First Dictionary

            排序 + 分离单词技巧

    #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;
    }
    


     

  • 相关阅读:
    Python Day14
    Python Day13
    Python Day12
    Python Day11
    Python Day10
    Python Day9
    Python Day8
    Python Day7
    Python Day6
    Python Day5
  • 原文地址:https://www.cnblogs.com/zcube/p/4194567.html
Copyright © 2011-2022 走看看