1. 程式人生 > >uva 156 - Ananagrams (反片語)

uva 156 - Ananagrams (反片語)

大小寫 距離 tor over ring not disk ces online

csdn:https://blog.csdn.net/su_cicada/article/details/86710107

例題5-4 反片語(Ananagrams,Uva 156)
輸入一些單詞,找出所有滿足如下條件的單詞:該單詞不能通過字母重排,得到輸入文
本中的另外一個單詞。 在判斷是否滿足條件時,字母不分大小寫,但在輸出時應保留輸入中
的大小寫,按字典序進行排列(所有大寫字母在所有小寫字母的前面)。
Sample Input
ladder came tape soon leader acme RIDE lone Dreis peat
ScAlE orb eye Rides dealer NotE derail LaCeS drIed
noel dire Disk mace Rob dries
#
Sample Output


Disk
NotE
derail
drIed
eye
ladder
soon

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=835&problem=92&mosmsg=Submission+received+with+ID+22713249

思路和書上的一樣, 先把每次的單詞變成小寫, 然後將其存入map的鍵, 值就是原單詞.
如果是重復的單詞, 那麽就把值變成空的.
最後判斷 map 裏每一個叠代對象的值是不是空就行了
將不是空的結果存入一個set, set自動排序, 然後再將set遍歷輸出即可

#include<iostream>
#include<map>
#include<string>
#include<set>
#include<algorithm>
using namespace std;

string lower(string s){
    string re;
    for(int i=0;i<s.size();i++){
        re += s[i]>=‘a‘ && s[i]<=‘z‘ ? s[i] : s[i] - ‘A‘ + ‘a‘; 
    }
    return re;
}

int main()
{
    map<string, string> dict;
    string word,temp;

    while(cin>>word && word != "#"){
        // cout<<word<<endl;
        temp = lower(word);
        // cout<<"lower "<<temp<<endl;
        sort(temp.begin(), temp.end());
        // cout<<"--"<<word<<endl;
        // cout<<"dict "<<dict[temp]<<endl;
        if(dict.count(temp) == 0){  //說明沒有
            dict[temp] = word;
        }else{ //有了我們就不要了
            dict[temp] = "";
        }
    }
    
    set<string> res;
    map<string, string>::iterator i = dict.begin();
    for(;i!=dict.end();i++){
        if((*i).second != ""){
            // cout<<(*i).second<<endl;
            res.insert((*i).second);
        }
    }
    for(set<string>::iterator i = res.begin(); i != res.end(); i++){
        cout<<(*i)<<endl;
    }

    return 0;
}
// AC at 2019/1/30

2019年第一道題, 還是例題, 距離上次做題已經有8個月以上了.
和去年相比實在是太散漫了, 是因為要學的東西多了嗎,是因為要做的事多了嗎,這不是借口,是我太廢物了. 好不容易活到這個等級, 不能gameover啊啊.

uva 156 - Ananagrams (反片語)