2015年7月31日 星期五

[UVA] 156 - Ananagrams

題意:
輸入很多字串,若該輸入的字串重組後不分大小寫
可以在之前輸入過字串中找到則不輸出。
例如:Apple banana orange appLE
則Apple和appLE都不輸出,
只輸出
banana
orange
要按照字典序!

方法:
map,若出現過的先用別的記號('#')取代,輸出時判斷是'#'則不輸出!


/* 20150731
 * hanting
 * UVa 156 - Ananagrams
 * C++
 */
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>//sort
using namespace std;
int main()
{
    map<string,string> Map;
    string str;
    while(cin>>str and str!="#")
    {
        string SortStr=str;
        transform(SortStr.begin(),SortStr.end(),SortStr.begin(),::tolower);
        sort(SortStr.begin(),SortStr.end());
        if(Map[SortStr].size()) Map[SortStr]="#";
        else Map[SortStr]=str;
    }
    vector<string> vec;
    for(map<string,string>::iterator it=Map.begin();it!=Map.end();++it)
    {
        if((it->second)!="#") vec.push_back(it->second);
    }
    sort(vec.begin(),vec.end());
    for(int i=0;i<vec.size();i++)
    {
        cout<<vec[i]<<endl;
    }
    return 0;
}

沒有留言:

張貼留言