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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章