UVA156 Ananagrams

问题链接:UVA156 Ananagrams

题意简述:输入一个文本文件,从中提取出一些单词输出,输出的单词按照文本文件中的原来样子输出(字母大小写不变)。对于所有的单词,若字母不分大小写,单词经过重排顺序,与其他单词相同,这些单词则不在输出之列。

这个问题用C++语言编写程序,主要是为了练习使用STL的功能。另外一点,C++编写程序效率会更高。

程序中,使用了容器类map和vector。其他都是套路。

AC的C++语言程序如下:

/* UVA156 Ananagrams */

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

map<string, int> dict;
vector<string> words;
vector<string> ans;

string getkey(const string& s)
{
    string key = s;

    for(int i = 0; i < (int)key.length(); i++)
        key[i] = tolower(key[i]);

    sort(key.begin(), key.end());

    return key;
}

int main()
{
    string s;

    while(cin >> s) {
        if(s[0] == '#')
            break;

        string key = getkey(s);

        if(dict.count(key) == 0)
            dict[key] = 0;
        dict[key]++;

        words.push_back(s);
    }

    for(int i=0; i<(int)words.size(); i++)
        if(dict[getkey(words[i])] == 1)
            ans.push_back(words[i]);

    sort(ans.begin(), ans.end());

    for(int i=0; i<(int)ans.size(); i++)
        cout << ans[i] << "\n";

    return 0;
}
时间: 2024-08-11 09:57:31

UVA156 Ananagrams的相关文章

UVa156 Ananagrams (STL)

链接:http://acm.hust.edu.cn/vjudge/problem/19294分析:map容器的应用.map容器按key值从小到大排序,所以自定义类型作为key时要定义“小于”运算符,还有map重载“[]”运算符,可以像数组一样使用.将每个单词“标准化”(将字母转换成小写并按字典序从小到大),cnt记录每个“标准化”后单词的个数,words保存出现过的所有单词,最后遍历words数组,将“标准化”后只出现一次的单词保存最后按字典序排序后输出. 1 #include <iostrea

【map练习】Uva156 - Ananagrams题解

今后,日常学习栏目中将专注于新知识本身的探讨,辅助练习题目如果关联不大就会放在基础练习中. 题意:要你找出这样的单词,单词里面的字母不区分大小写,然后字母重排之后只出现一次的单词,找出之后,还是输入的样子以字典序输出 读了ruka上的标程后,自己写了一遍,代码如下: #include<iostream> #include<cstdio> #include<string> #include<map> #include<vector> #includ

UVa-156 Ananagrams(map映射)

参考:https://blog.csdn.net/hoaresky1998/article/details/51351588 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <set> 5 #include <map> 6 #include <algorithm> 7 using namespace std; 8 map<stri

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

map映射

map实例代码: 1 // UVa156 Ananagrams 2 // Rujia Liu 3 // 题意:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排得到输入文本中的另外一个单词 4 // 算法:把每个单词“标准化”,即全部转化为小写字母然后排序,然后放到map中进行统计 5 #include<iostream> 6 #include<string> 7 #include<cctype> 8 #include<vector> 9 #i

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

Winter-2-STL-F Ananagrams 解题报告及测试数据

Time Limit:3000MS     Memory Limit:0KB 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,

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

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