• 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] << "
    ";
    
        return 0;
    }


  • 相关阅读:
    mysql索引
    数据库修复
    数据库取值 三级分类后台遍历
    创建数据库!
    mysql按条件 导出sql
    nodejs 简单安装环境
    C++ 性能剖析 (一)
    C++ 性能剖析 (二):值语义 (value semantics)
    JavaScript Nested Function 的时空和身份属性
    C++ Reference 的“三位一体”诠释
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7564471.html
Copyright © 2020-2023  润新知