木其网络科技专业程序员代写http://www.xmsydw.com
程序员学历擅长经验网店链接
apenny硕士ASP.NET PHP 电子 通信设计 图像 编程 网络5年进入店铺
zheng_qianqian本科C语言 C++面向对象 Java5年进入店铺
guoguanl本科Java Web项目 JSP Hibernate Struts Mysql5年进入店铺

字符串系列——词典排序


Problem B: Andy's First Dictionary

Time limit: 3 seconds


Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. From his bookshelf he would pick one of his favourite story books, from which he would copy out all the distinct words. By arranging the words in alphabetical order, he is done! Of course, it is a really time-consuming job, and this is where a computer program is helpful.

You are asked to write a program that lists all the different words in the input text. In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case. Words with only one letter are also to be considered. Furthermore, your program must be CaSe InSeNsItIvE. For example, words like "Apple", "apple" or "APPLE" must be considered the same.

Input

The input file is a text with no more than 5000 lines. An input line has at most 200 characters. Input is terminated by EOF.

Output

Your output should give a list of different words that appears in the input text, one in a line. The words should all be in lower case, sorted in alphabetical order. You can be sure that he number of distinct words in the text does not exceed 5000.

Sample Input

Adventures in Disneyland

Two blondes were going to Disneyland when they came to a fork in the
road. The sign read: "Disneyland Left."

So they went home.

Sample Output

a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when


咋一看好像是挺水的一题,好像只要一个一个录入单词再排序输出就行了。所以我先做这题(挑简单的做。。。)

于是开始编就发现没那么简单:

1.录入单词不能用scanf,因为它不能跳过各种符号。于是选择用getchar。

2.还有得判断那个词是不是已经录入过,重复录入就不好了。

有两种思路:一是先全部录入,排序后输出时判断有没有重复,不输出重复字符;二是录入时判断数组里面有没有重复,没重复再进行录入。

很明显第二种在排序输出是比较简单,果断选择第二种。


Ac代码如下:

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

int main()
{
    int cnt = 0, i, j;
    char ch[5000][50], temp[50], tmp;

    while(1)
    {
        if ((tmp = getchar())== EOF)
            break;
        if (isalpha(tmp))
            tmp = tolower(tmp);
        else
            continue;
        memset(temp, 0, sizeof(temp));
        for (i = 0; 1; i ++)
        {
            temp[i] = tmp;
            if (!isalpha(tmp = tolower(getchar())))
            {
                for (j = 0; j < cnt + 1; j ++)
                {
                    if (strcmp(temp, ch[j]) == 0)
                        break;
                    else if (j == cnt)
                    {
                        strcpy(ch[cnt], temp);
                        cnt ++;
                        break;
                    }
                }
                //printf("cnt = %d, temp = %s, ch[cnt-1] = %s\n", cnt, temp, ch[cnt-1]);
                break;
            }
        }
    }
    for (i = 0; i < cnt - 1; i ++)
    {
        for (j = i + 1; j < cnt; j ++)
        {
            if(strcmp(ch[i], ch[j]) < 0)
            {
                strcpy(temp, ch[i]);
                strcpy(ch[i], ch[j]);
                strcpy(ch[j], temp);
            }
        }
    }


    while (cnt --)
        printf("%s\n", ch[cnt]);

    return 0;
}



posted @ 2013-03-08 21:30  程序流程图  阅读(224)  评论(0编辑  收藏  举报
木其网络科技专业程序员代写http://www.xmsydw.com
程序员学历擅长经验网店链接
apenny硕士ASP.NET PHP 电子 通信设计 图像 编程 网络5年进入店铺
zheng_qianqian本科C语言 C++面向对象 Java5年进入店铺
guoguanl本科Java Web项目 JSP Hibernate Struts Mysql5年进入店铺