UVA – 156 – Anangrams

  • Problem Statement : http://acm.uva.es/p/v1/156.html
  • Type : Adhoc, STL Libraries.
  • Hints :
    • There is an object called multimap that allows multi entries with the same key, clean each string; convert it to lower case and sort it and then insert it with the clean key. then use the count method to count each key in the multimap.
    • To convert a string into it’s lower case you can use the transform method .. it takes an iteratable input and output and a transform function. It’s more clean and neat than a for loop 🙂
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
#include<cstdio>
#include<cctype>
#include<string.h>
using namespace std;
typedef multimap<string, string>::iterator Iterator;

int main() {

	multimap<string, string> m;
	string t;
	char c[200];
	while (scanf("%s", c)==1 && strcmp(c, "#")) {
		string s(c), clean(t);
		transform(s.begin(), s.end(), clean.begin(), ::tolower);
		sort(clean.begin(), clean.end());
		m.insert(make_pair(clean, s));
	}

	vector<string> ans;
	for (Iterator i = m.begin(); i != m.end(); i++)
		if (m.count(i->first) == 1)	ans.push_back(i->second);

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

	for (size_t i = 0; i < ans.size(); i++) printf("%s\n", ans[i].c_str());

	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: