感觉这道题要比之前几个字符串处理的题目难度要大了一些。

题目大意:给若干行字符串,提取出所有单词并去掉重复的,最后按字典顺序输出。

对于输入大致有两种思路,一种是逐个读入字符,遇到字母的话就放到word里面,直到遇到非字母字符,在word最后放'\0'。

我采用第二种思路,就是用gets()函数逐行读到str字符数组里面,然后逐个把单词提取出来。

Count存放的是Dictionary里单词的个数,每提取一个单词,看看Dictionary里有没有该单词,没有的话放到字典里面,并且计数器加1。

由于功力不够深厚,最后对Dictionary排序的时候不会写cmp函数,因此用不了qsort(),所以又笨笨的写了个冒泡排序,居然没有超时。

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

国际惯例,AC代码如下:

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; char Dictionary[][];//用来存放单词
int Count = ; //不重复的单词的个数
char str[], word[]; int cmp(const void *a, const void *b); int main(void)
{
#ifdef LOCAL
freopen("10815in.txt", "r", stdin);
#endif int i, j;
while(gets(str))
{
int l = strlen(str);
if(l == )
continue;
for(i = ; i < l; ++i)
str[i] = tolower(str[i]);
i = ; while(i < l)
{
if((i == || str[i - ] < 'a' || str[i - ] > 'z')//判断一个单词的开始
&& str[i] >= 'a' && str[i] <= 'z')
word[] = str[i];
else
{
++i;
continue;
} ++i;
int j = ;
while(str[i] >= 'a' && str[i] <= 'z' && i < l)
{
word[j++] = str[i++];
}
word[j] = '\0'; for(j = ; j < Count; ++j)
{
if(!strcmp(Dictionary[j], word))
break;
}
if(j == Count)
strcpy(Dictionary[Count++], word);
}
} for(i = Count - ; i > ; --i)//不会用qsort,只能恶心地写个冒泡了
{
for(j = ; j < i; ++ j)
{
if(strcmp(Dictionary[j], Dictionary[j + ]) > )
{
char t[];
strcpy(t, Dictionary[j]);
strcpy(Dictionary[j], Dictionary[j + ]);
strcpy(Dictionary[j + ], t);
}
}
}
for(i = ; i < Count; ++i)
cout << Dictionary[i] << endl;
return ;
}

代码君

最后去网上查了一下别人写的cmp函数,试了一下同样AC!

 int cmp(const void *a, const void *b)
{
return strcmp((char *)a, (char *)b);
}
qsort(Dictionary, Count, sizeof(Dictionary[]), cmp);

以上是我几个月前写的代码,现在看起来真挫啊!学习一下紫书STL的用法。最大的特点就是用起来方便而且代码简短不容易出错,而且速度也比我的代码快多了,以上我手写的冒泡排序运行时间为0.256s,加上快排的话大概能缩短到0.1s左右,但这份STL代码仅仅0.062s

set就是数学上的集合,每个元素都不重复。同sort一样,自定义类型也可以构造set,但同样必须定义“小于”运算符。

 //#define LOCAL
#include <iostream>
#include <set>
#include <string>
#include <sstream>
using namespace std; set<string> dict; int main(void)
{
#ifdef LOCAL
freopen("10815in.txt", "r", stdin);
#endif string s, buf;
while(cin >> s)
{
for(int i = ; i < s.length(); ++i)
if(isalpha(s[i])) s[i] = tolower(s[i]); else s[i] = ' ';
stringstream ss(s);
while(ss >> buf) dict.insert(buf);
}
for(set<string>::iterator it = dict.begin(); it != dict.end(); ++it)
cout << *it << "\n"; return ;
}

飞快的代码君

UVa 10815 Andy's First Dictionary的更多相关文章

  1. UVA - 10815 - Andy's First Dictionary STL

    Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him ...

  2. UVA 10815 Andy's First Dictionary (C++ STL map && set )

    原题链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  3. UVA 10815 Andy's First Dictionary ---set

    题目链接 题意:输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出.单词不区分大小写. 刘汝佳算法竞赛入门经典(第二版)P112 #include <iostream> ...

  4. UVA 10815 Andy's First Dictionary【set】

    题目链接:https://vjudge.net/contest/211547#problem/C 题目大意: 输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出,单词不区分大小写 ...

  5. Uva 10815 Andy's First Dictionary(字符串)

    题目链接:https://vjudge.net/problem/UVA-10815 题意 找出一段文本中的所有单词,以小写形式按照字典序输出. 思路 用空白符替换文本中所有非字母字符后再次读入. 代码 ...

  6. UVA 10815 Andy&#39;s First Dictionary(字符处理)

    Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him ...

  7. UVa10815.Andy's First Dictionary

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. UVA-10815 Andy's First Dictionary (非原创)

    10815 - Andy's First Dictionary Time limit: 3.000 seconds Problem B: Andy's First DictionaryTime lim ...

  9. 【UVA - 10815】Andy's First Dictionary (set)

    Andy's First Dictionary Description 不提英文了 直接上中文大意吧 XY学长刚刚立下了再不过CET就直播xx的flag,为了不真的开启直播模式,XY学长决定好好学习英 ...

随机推荐

  1. Javacript中(function(){})() 与 (function(){}()) 区别 {转}

    这个问题可以从不同的角度来看,但从结果上来说 :他们是一样的.首先,如果从AST(抽象语法树)的角度来看,两者的AST是一模一样的,最终结果都是一次函数调用.因此,就解析器产生的结果论而言,两者是没有 ...

  2. Codis集群的搭建与使用

    一.简介 Codis是一个分布式的Redis解决方案,对于上层的应用来说,连接Codis Proxy和连接原生的Redis Server没有明显的区别(不支持的命令列表),上层应用可以像使用单机的Re ...

  3. 创建本地yum源及grouplist 出错

    RHEL有时候使用自定义的YUM源是很方便的事情. yum install createrepo createrepo /your/repo/directory/ 不过由于粗心,本人在使用时遇到很郁闷 ...

  4. jboss 占用cpu 100%

    通过Java thread dump分析找到耗费CPU最高的源代码 分类: 9. Java2010-04-11 23:06 9272人阅读 评论(4) 收藏 举报 threadjavaeclipse插 ...

  5. C# static方法-使用迭代器循环遍历文件中的额行

    //封装的方法 //读取文件的值,放入集合中 public static IEnumerable<string> ReadLines(string fileName) { using (T ...

  6. C# 中Newtonsoft.Json的安装和使用

    官网参考:http://json.codeplex.com/ 在程序包管理控制台,键入NuGet命令  install-package Newtonsoft.Json  安装Newtonsoft.Js ...

  7. PHP使用SOAP调用.net的WebService数据

    需要和一个.net系统进行数据交换,对方提供了一个WebService接口,使用PHP如何调用这个数据呢,下面就看看使用SOAP调用的方法吧 这个与一般的PHP POST或GET传值再查库拿数据的思路 ...

  8. 为什么toString方法可以用来区分数组和对象?

    首先大家都应该知道在javascript中只有是对象都存在toString方法,将调用该方法的值转换为字符串返回,如下: var arr = [1, 2, 3]; console.log(arr.to ...

  9. jenkins配置及使用中出现的问题

    安装中遇到的问题: 1.linux中最好用普通用户安装tomcat和jenkins,用普通用户启动tomcat,否则jenkins工作空间不会在普通用户下,而线上自动发布部署时,是不允许用root用户 ...

  10. [转载]Jmeter那点事·ForEach和If控制器

    如果我们要实现一个循环,如果城市是北京,则返回首都:否则,返回城市.   一.新建用户自定义变量 添加-配置元件-用户自定义变量, 定义变量注意命名格式:变量名 加 下划线 加 数字(从1开始计数) ...