2015年7月30日 星期四

[UVA] 10815 - Andy's First Dictionary

題意:
輸入很多字串,
將字串以字典序且不重複的輸出,
輸出的字串都要小寫。
注意:what's  這樣是兩個字串  what  ,  s
要個別輸出!

方法:
讀字元方式一個字元一個字元讀進來,存進str裡面
遇到非字母字元(ex: " , @ # ...)就將str放進set裡,再將str清空!

/* 20150731
 * hanting
 * UVa 10815 - Andy's First Dictionary
 * C++
 */
#include <iostream>
#include <set>
using namespace std;
int main()
{
    set<string> Set;
    string str="";
    char ch;
    while(cin.get(ch))
    {
        if(isalpha(ch))//是字母
        {
            str+=tolower(ch);//變小寫
        }
        else if(str.size())
        {
            Set.insert(str);
            str="";
        }
    }
    for(set<string>::iterator itr=Set.begin();itr!=Set.end();++itr)
    {
        cout<<*itr<<endl;
    }
    return 0;
}

沒有留言:

張貼留言