问题链接: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;
}