10815 Andy’s First Dictionary
題目原文
題目說明
輸入一段文章,輸出每一單字,並按字典序排列。
思路
一個單字一個單字去讀,若遇到像 andy's 這種詞,須將其切割為 andy 和 s 兩個單字,
讀完丟入 set (會自動依字典序排列),再利用迭代器輸出即可。
題目說明
輸入一段文章,輸出每一單字,並按字典序排列。
思路
一個單字一個單字去讀,若遇到像 andy's 這種詞,須將其切割為 andy 和 s 兩個單字,
讀完丟入 set (會自動依字典序排列),再利用迭代器輸出即可。
#include<iostream>
#include<cctype>
#include<cstring>
#include<sstream>
#include<set>
using namespace std;
set<string> dict;
int main()
{
string input;
while(cin >> input)
{
for(int i = 0; i < input.length(); i++)
{
if(isalpha(input[i]))
input[i] = tolower(input[i]);
else
input[i] = ' '; // 非字母轉成空白字元 (ex. andy's 變 andy s)
}
stringstream ss; // 拿來切割 (ex. andy s 變兩個字串)
ss << input;
string output;
while(ss >> output)
dict.insert(output); // 會自動由小到大排
}
set<string>::iterator it;
for(it = dict.begin(); it != dict.end(); it++)
cout << *it << endl;
return 0;
}
留言
張貼留言