安迪的第一個字典(UVa 10815)
阿新 • • 發佈:2019-02-07
【問題描述】輸入一個文字,找出所有不同的單詞(連續的字母序列),按字典序從小到大輸出。單詞不區分大小寫。
【樣例輸入】
Adventures in Disneyland
Two blondes were going to Disneyland when they came to a fork in the
road. The sign read: "Disneyland Left."
So they went home.
【樣例輸出】
aadventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when
#include<iostream> #include<string> #include<set> #include<sstream> using namespace std; set<string> dict; //為字典設定一個名為dict-short的集合,它基於字串型別; int main(){ string s,buf; while (cin>>s){ for (int i=0;i<s.length();i++) if (isalpha(s[i])) //isalpha函式:判斷字元ch是否為英文字母,若為英文字母,返回非0.若不是字母,返回0 s[i]=tolower(s[i]); //如果它是一個字母,把它變成小寫。 else s[i]=' '; stringstream ss(s); //如果是空格,忽略它 while (ss>>buf) dict.insert(buf); //插入到已經是有序的集合,TYkon說這是一個平衡的樹裡面 (不懂) } for (set<string>::iterator it=dict.begin();it!=dict.end();++it)//迭代器就像一個點,從頭到尾掃描它並輸出 (不懂) cout<<*it<<endl; //NOTICE按點輸出 return 0; }