uva 156 - Ananagrams (反片语)

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啊啊.

原文地址:https://www.cnblogs.com/SuCicada/p/10340288.html

时间: 2024-11-12 04:38:06

uva 156 - Ananagrams (反片语)的相关文章

UVA 156 Ananagrams 关于二维数组表示的字符串排序的问题

题目链接:UVA 156 Ananagrams  Ananagrams  Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how

UVa 156 - Ananagrams

 Ananagrams  Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their let

UVa 156 Ananagrams(STL,map)

 Ananagrams  Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their let

Ananagrams UVA 156

题目: Ananagrams Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their l

STL语法——映射:map 反片语(Ananagrams,UVa 156)

Description Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their lett

【UVa 156】Ananagrams

 Ananagrams  Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their let

Ananagrams(反片语)【map的应用】P114

中文题意:输入一些单词,找出满足如下条件的单词:该单词不能通过字母重排,得到文本中的另一个单词.在判断是否满足条件时,字母不分大小写,但在输出是应保留输入中的大小写,按字典序进行排序(所有大写字母在所有小写字母的前面).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 # #i

UVa 156 (stl map的使用)

  一.map map就是从键(key)到值(value)的映射,重载了[]所以可以认为是高级版的数组,常用的一些操作如下: 头文件:#include<map> 定义:map <类型一(key),类型二(value)> name  key称为map的frist,value称为map的second. 初始化:name.clear(); 二.题目 Ananagrams Most crossword puzzle fans are used to anagrams--groups of

156 - Ananagrams

Ananagrams Most crossword puzzle fans are used to anagrams–groups of words with the same letters in different orders–for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their letters