UVA – 195 – Anagrams

  • Problem Statement :
  • Type : Permutations, Anagrams
  • Gochas : change the compare function of the
  • Dirty Note : instead of using the tolower() method and incase you noticed those binary representations, you will then have to notice that to convert a letter to its lower Case all you have to do is XOR it with 1<<5 or (32).   i.e

 letter = (letter | 32); 

Letter Binary
A 0100 0001
Z 0101 1010
a 0110 0001
z 0111 1010

#include <algorithm>
#include <iostream>
#include <cctype>
using namespace std;
string s;
bool comp(const char &a, const char &b) {
	int delta = tolower(a) - tolower(b);
	return delta ? delta < 0 : a <b;
int main() {
	int T;
	while (T-- && cin >> s) {
		sort(s.begin(), s.end(), comp);
			cout << s << endl;
		while (next_permutation(s.begin(), s.end(), comp));
	return 0;


One thought on “UVA – 195 – Anagrams

  1. Fabho says:

    Thank you very much….. I was with problems with the sort function, now is more clearly, but can u explain a few more your function????

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: