例题5-3 安迪的第一个字典 UVa10815

算法竞赛入门经典(第2版) 第5C++与STL入门

题5-3安迪的第一个字典  UVa10815

感悟。

1、从网站下载英文原题,重点在看输入输出数据与格式。

2英文还算简单,对照输入输出能很快理解。

3、初步计划scanf读取,去除非字母字符,将大写统该小写,但数组插入操作麻烦,准备查一查set用法,再编码。

4、以上编码很快实现,在存储单词停了下来,插入删除比较麻烦。

5、参考了http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/13/2636375.html的set用法,准备用set来充当单词容器。之后再进行排序。

6、如何读取set中元素,参考了http://blog.csdn.net/chaiwenjun000/article/details/50561775学习set遍历,即获取set中的元素,猜测字符串按字典序列排序。

7、编译运行,太强悍了,只需把单词往set里加,果然是按字典序列排序,其它什么事都不用做,简单方便。

8、http://vjudge.net提交WA,怎么可能?

9、从http://blog.csdn.net/thudaliangrx/article/details/50707688拷贝代码,提交AC,开始对拍,立马发现问题,下面提供测试数据:

10815 - Andy's First Dictionary

Time limit: 3.000 seconds

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
10、经过对拍发现//10815  Andy's "APPLE" 容易出错,修改有一定难度,想想。

11、http://vjudge.Net提交AC,从修改过程来看,不对拍,这个错误是查不出的,接下来上https://uva.onlinejudge.org/提交AC。此时2016-11-15 20:16

12、放心看书中的代码,处理单词这部分确实写得比本人好,非字统统编程空格,通过字符流再读取。

附上AC代码,编译环境Dev-C++4.9.9.2

#include <cstdio>
#include <set>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;

int main(){
    char str[210];
    char s[210];
    int len;
    int i,j;
    int scount;
    set<string> sset;
    int setcount;
    set<string>::iterator it;
    while(scanf("%s",str)!=EOF){//10815  Andy's "APPLE" 容易出错
        len=strlen(str);
        scount=0;
        for(i=0;i<len;i++){//两个功能,一去除标点,二字母全部转化为小写
            if(str[i]>='a'&&str[i]<='z')
                s[scount++]=str[i];
            else if(str[i]>='A'&&str[i]<='Z')
                s[scount++]='a'+str[i]-'A';
            else{//非英文字母,不再往下遍历
                s[scount]='\0';
                if(strlen(s)>0)
                    sset.insert(s);
                scount=0;
            }
        }
        s[scount]='\0';//字符串结尾
        if(strlen(s)>0)//防止空字符串
            sset.insert(s);
    }
    for(it=sset.begin();it!=sset.end();it++)
        cout<<*it<<"\n";

    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值