UVA - 10815 - Andy's First Dictionary

来源:互联网 发布:淘宝云客服培训 编辑:程序博客网 时间:2024/06/05 09:09

题意:

    输入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_TESTint main(){#ifdef LOCAL_TESTfreopen("..\\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, automaticallyfor ( set<string>::iterator it=setRes.begin(); it!=setRes.end(); it++ )cout << *it <<endl;return 0;}


0 0