uvaoj 156 Ananagrams map的基本使用

uvaoj 156 Ananagrams map的基本使用
给定一些单词,找出满足如下条件的单词:该单词不能通过字母重排,得到文本中的另外其他单词,在判断满足条件时,不区分大小写,但输出时应该保留输入中的大小写。
我们可以将单词都变成小写,然后将单词中的字母按照大小排列,那些不满足条件的单词就会出现多次,而满足条件的单词只会出现一次,因为要记录之前的单词,可以通过map集合来映射。
代码如下:
/*************************************************************************
	> File Name: 156.cpp
	> Author: gwq
	> Mail: [email protected] 
	> Created Time: 2015年01月20日 星期二 14时59分25秒
 ************************************************************************/

#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>

#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())

using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;

const double esp = 1e-5;

map<string, int> cnt;
vector<string> words;

string repr(const string &s)
{
	string ans = s;
	int len = s.length();
	for (int i = 0; i < len; ++i) {
		ans[i] = tolower(ans[i]);
	}
	sort(ans.begin(), ans.end());
	return ans;
}

int main(int argc, char *argv[])
{
	string s;
	while (cin >> s) {
		if (s[0] == '#') {
			break;
		}
		words.pb(s);
		string r = repr(s);
		if (cnt.count(r) == 0) {
			cnt[r] = 0;
		}
		cnt[r]++;
	}
	vector<string> ans;
	int len = words.size();
	for (int i = 0; i < len; ++i) {
		if (cnt[repr(words[i])] == 1) {
			ans.pb(words[i]);
		}
	}
	sort(ans.begin(), ans.end());
	len = ans.size();
	for (int i = 0; i < len; ++i) {
		cout << ans[i] << endl;
	}

	return 0;
}


发布了124 篇原创文章 · 获赞 15 · 访问量 10万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章