Monday, February 7, 2011

TopCoder SRM 496, DIV II, Anagram Free

Problem Statement

A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb".

A set of strings is anagram-free if it contains no pair of strings which are anagrams of each other. Given a set of strings S, return the size of its largest anagram-free subset. Note that the entire set is considered a subset of itself.


#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <bitset>
#include <algorithm>
#include <sstream>
#include <cstring>
using namespace std;
typedef long long int lli;
typedef long unsigned long llu;

//definitions
#define mem_prep(varx) memset(varx,0,sizeof(varx))
#define all(x)  (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define sz(v) ((int)(v).size())
#define rep(i,a,n) for(int i=(a);i<(n);i++)
#define repe(i,a,n)  for(int i=(a);i<=(n);i++)
#define forsz(i,a,v) rep(i,a,sz(v))
#define repsz(i,v) rep(i,0,sz(v))
#define vi vector<int>
#define vs vector<string>

bool myfunc(string i, string j){
  if (i<j) return true;
  return false;
}

class AnagramFree {

 
public:

int getMaximumSubset(vector <string> S) {
 int ans=0;
 repsz(i,S)sort(all(S[i]));
 sort(S.begin(), S.end() ,myfunc);
 ans++;
 string s=S[0];
 forsz(i,1,S){
   if(S[i]==s)continue;
   else s=S[i],ans++;
 }
 return ans;
}
};

No comments:

Post a Comment