#UVa:10815-Andy’s First Dictionary

灆洢 2015-11-27 11:34:43

一個字一個字去讀,將連續的英文字連在一起形成單字再丟到集合內,最後將集合內的字串排序輸出即可。

P.S. 注意像是time-consuming或是andy’s這樣的詞要當作time、consuming以及andy、s這樣的雙單字來看。

C++(0.013)

/*******************************************************/
/* UVa 10815 Andy's First Dictionary                   */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2015/11/27                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <cctype>
#include <string>
#include <set>
using namespace std;

int main(){
  set<string> dictionary;
  char word;
  string alphaWord;
  while( scanf("%c", &word) != EOF ){
    if( isalpha(word) ){
      alphaWord += tolower(word);
    }
    else if( alphaWord != "" ){
      dictionary.insert(alphaWord);
      alphaWord = "";
    }
  }

  if( alphaWord != "" ){
    dictionary.insert(alphaWord);
  }

  for( set<string>::iterator it = dictionary.begin() ; it != dictionary.end() ; it++ ){
    printf("%s\n", it->c_str());
  }

  return 0;
}

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料