# Andy’s First Dictionary(UVA10815) ## 題目 [點我](https://onlinejudge.org/external/108/10815.pdf) ## 解題網站 [UVA](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1756) ZJ上無此題 ## 演算法 根據原文,word的定義是:"a word is defined as a consecutive sequence of alphabets, in upper and/or lower case.",所以連續的英文字母就是一個word,使用map,因為map會自動排序(用set也可以),並且會去除重複的元素,這是我們要的 ``` 1. 宣告map mp,key代表出現過的word,value沒有意義 2. 當輸入str: 2.1. 宣告word,一開始為空字串 2.2. 掃過str: 2.2.1. 如果str的元素為大寫,轉為小寫後,放入word的尾端,如果str的元素為小寫,放入word的尾端,否則如果word不為空字串,將word放入mp中,再將word設為空字串 2.3. 若word不為空,將word放入mp中 3. 掃過mp,輸出mp的first ``` ## 程式碼 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { map <string, int> mp; string str; while (cin >> str) { string word; for (int i = 0; i < str.length(); i++) { if ('A' <= str[i] and str[i] <= 'Z') { str[i] += 32; word += str[i]; } else if ('a' <= str[i] and str[i] <= 'z') { word += str[i]; } else if (word != ""){ mp[word] = 1; word = ""; } } if (word != "") { mp[word] = 1; } } for (auto it = mp.begin(); it != mp.end(); it++) { cout << it -> first << endl; } return 0; } ```