UVA - 10815 - Andy's First Dictionary

题意:

    输入N行数据,将其中所有单词抽出,形成一个正序字典;最后输出整个字典。


注意:

    ① 不会大小写,统一输出小写;

    ② 非字母字符不能输出

    ③ 单词多长多短,只要隔开,就是一个单词。


思路:

    以“行”为单位,进行处理。

    ① 输入一行,保存到char*中

    ② char*转string

    ③ 大写字母换成小写

    ④ 非字母字符换成空格

    ⑤ istringstream流逐个输出各单词

    ⑥ 保存到set中;set自动去重及正序化。(注:multiset可重复)

    最后输出整个set。


注意:

    ① set自动去重及正序化;而multiset可重复

    ② 多用英语表达。


#include <iostream>
#include <sstream>
#include <set>
#include <algorithm>
#include <string>
#include <stdio.h>
using namespace std;

// #define LOCAL_TEST

int main()
{
#ifdef LOCAL_TEST
	freopen("..\\in.txt", "r", stdin);
	freopen("..\\out.txt", "w+", stdout);
#endif

// ------- Solution by using vector + Sync(false)
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);

	char strIn[210];
	set <string> setRes;

	while ( gets(strIn) )
	{
		// Transform char* To string. 
		string strLine(strIn);
		// Using transform To LOWER all charactors of the string. 
		transform(strLine.begin(), strLine.end(), strLine.begin(), ::tolower);

		// Set all non-letter To blank space. 
		for ( string::iterator it=strLine.begin(); it != strLine.end(); it++ )
			if ( !isalpha(*it) )
				*it = ' ';

		// Use istringstream to separate all words,
		// Meanwhile, store into setRes(SET)
		istringstream strStream(strLine);
		string strOut;
		while ( strStream >> strOut )
			setRes.insert(strOut);
	} // end while

	// Output all words in setRes(SET).
	// PS, set is sorted alphabetically, automatically
	for ( set<string>::iterator it=setRes.begin(); it!=setRes.end(); it++ )
		cout << *it <<endl;

	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值