Andy's First Dictionary STL练习C++题解

Andy’s First Dictionary STL练习C++题解

Time limit:10000 ms Memory limit:262144 kB

描述

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.

输入

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.

输出

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.

题目大意

8岁的安迪有一个梦想——他想自己编一本字典。对他来说,这不是一件容易的事情,因为他知道的单词数量还不够。他没有把所有的话都想出来,而是有一个聪明的想法。他会从书架上挑一本他最喜欢的故事书,从中抄写所有不同的单词。按字母顺序排列单词,他就完了!当然,这是一项非常耗时的工作,而这正是计算机程序有帮助的地方。您需要编写一个程序,列出输入文本中的所有不同单词。在这个问题中,一个单词被定义为字母的连续序列,大小写为大写和/或小写。只有一个字母的单词也要考虑。此外,您的程序必须不区分大小写。例如,“apple”、“apple”或“apple”等词必须视为相同。

输入

输入文件是不超过5000行的文本。输入行最多有200个字符。输入被EOF终止。

输出

您的输出应该给出显示在输入文本中的不同单词的列表,一行一个。所有单词都应为小写,按字母顺序排序。您可以确定,文本中不同单词的数目不超过5000。

样例输入

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.

样例输出

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

代码

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

#define PI 3.14159265358979383246
#define LL long long

#define _for(i, a) for(size_t i = 0; i < (a); ++i)
#define _rep(i, a, b) for(size_t i = (a); i < (b); ++i)

using namespace std;

int main() {
	set<string> a;//用来存储小写字符串
	string ins;
	char x[2010], y[2010];//缓存要存进数组里的数据
	while (cin >> x) {
		int j = 0;
		_for(k, strlen(x)) {
			if (x[k] >= 91 && x[k] <= 96 || x[k] >= 33 && x[k] <= 46 || x[k] >= 58 && x[k] <= 64 || x[k] >= 123 && x[k] <= 126) {
				y[j] = '\0';
				a.insert(y);
				j = 0;
				continue;
			}
			if (isalpha(x[k])) {
				y[j++] = tolower(x[k]);
			}
		}
		y[j] = '\0';
		a.insert(y);

		ins = *(a.begin());
		if (!ins.size())
			a.erase(a.begin());
	}
	检验获取的数据
	//cout << *(a.begin()) << '-';
	//_for(i, a.size()) {
	//	cout << "###";
	//	_for(j, a[i].size()) {
	//		cout << a[i][j];
	//	}
	//	cout << endl;
	//}

	set<string>::iterator iter = a.begin();
	while (iter != a.end())
	{
		cout << *iter << endl;
		iter++;
	}

	return 0;
}

本人也是新手,也是在学习中,勿喷

转载请注明出处

欢迎有问题的小伙伴一起交流哦~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值