195 - Anagram

All about problems in Volume I. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

195 - Anagram

Postby @ce » Wed Jun 06, 2012 6:04 pm

Whats wrong with my code...plzz help

Code: Select all
Code removed after AC
Last edited by @ce on Mon Jun 25, 2012 10:01 am, edited 2 times in total.
-@ce
User avatar
@ce
Learning poster
 
Posts: 66
Joined: Mon May 28, 2012 8:46 am
Location: Ranchi, India

Re: 195 - Anagram

Postby brianfry713 » Wed Jun 06, 2012 10:31 pm

Input:
Code: Select all
1
aAbB


AC output:
Code: Select all
AaBb
AabB
ABab
ABba
AbaB
AbBa
aABb
aAbB
aBAb
aBbA
abAB
abBA
BAab
BAba
BaAb
BabA
BbAa
BbaA
bAaB
bABa
baAB
baBA
bBAa
bBaA
brianfry713
Guru
 
Posts: 1856
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 195 - Anagram

Postby @ce » Thu Jun 14, 2012 4:45 pm

Now, getting the cases right but getting TLE :(

Code: Select all
Code removed after AC
Last edited by @ce on Mon Jun 25, 2012 10:01 am, edited 1 time in total.
-@ce
User avatar
@ce
Learning poster
 
Posts: 66
Joined: Mon May 28, 2012 8:46 am
Location: Ranchi, India

Re: 195 - Anagram

Postby brianfry713 » Thu Jun 14, 2012 10:20 pm

Doesn't match the sample I/O.
brianfry713
Guru
 
Posts: 1856
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 195 - Anagram

Postby @ce » Fri Jun 15, 2012 2:02 pm

I think it does..
-@ce
User avatar
@ce
Learning poster
 
Posts: 66
Joined: Mon May 28, 2012 8:46 am
Location: Ranchi, India

Re: 195 - Anagram

Postby brianfry713 » Fri Jun 15, 2012 8:59 pm

http://ideone.com/YIZBW

Your output has 36 lines, the sample output has 24 lines.
brianfry713
Guru
 
Posts: 1856
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 195 - Anagram

Postby @ce » Fri Jun 15, 2012 9:43 pm

I am sorry, I had made a stupid mistake.
Thanx brianfry...i got AC :)
-@ce
User avatar
@ce
Learning poster
 
Posts: 66
Joined: Mon May 28, 2012 8:46 am
Location: Ranchi, India

Re: 195 - Anagram

Postby mgavin2 » Tue Feb 12, 2013 9:55 pm

WA ... I'm not sure anymore with the hacks I've tried to work around :\

Code: Select all
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cctype>
#include <set>

using namespace std;

//#define DEBUG
//#undef DEBUG //uncomment this line to pull out print statements
#ifdef DEBUG
#define TAB '\t'
#define debug(a, end) cout << #a << ": " << a << end
#else
#define debug(a, end)
#endif

typedef pair<int, int> point;
typedef long long int64; //for clarity
typedef vector<int> vi; //?
typedef vector<point> vp; //?
template<class T> void chmin(T &t, T f) { if (t > f) t = f; } //change min
template<class T> void chmax(T &t, T f) { if (t < f) t = f; } //change max

#define UN(v) SORT(v),v.erase(unique(v.begin(),v.end()),v.end())   
#define SORT(c) sort((c).begin(),(c).end())   
#define FOR(i,a,b) for (int  i=(a); i < (b); i++)   
#define REP(i,n) FOR(i,0,n)   
#define CL(a,b) memset(a,b,sizeof(a))
#define CL2d(a,b,x,y) memset(a, b, sizeof(a[0][0])*x*y)

/*global variables*/
struct compare
{
    bool operator() (const string& a, const string& b)
    {
        bool t = false;
        for (size_t i = 0; i < a.length() && i < b.length(); ++i)
            if (toupper(a[i]) < toupper(b[i]))
                return true;
            else if (toupper(a[i]) > toupper(b[i]))
                return false;
            else if (toupper(a[i]) == toupper(b[i]))
                if ( a[i] < b[i] )
                    return true;
        /*t = a[i] < b[i] ? true : false;

          return t;*/

    }
};

struct comp
{
    bool operator() (const char& a, const char& b)
    {
        debug(a, TAB); debug(b, TAB); debug((isupper(a) ? a < b : b > a), endl);
        return (isupper(a) ? a < b : b > a);
    }
};
string word;
/*global variables*/

void dump()
{
    //dump data
}

bool getInput()
{
    //get input
    cin >> word;
   
    return true;
}

void process()
{
    //process input
    set<string, compare> words;
    //SORT(word);
    do
    {
        words.insert(word);
        debug(word, endl);
    }
    while (next_permutation(word.begin(), word.end(), comp()));

    //quick hack.
    do
    {
        words.insert(word);
        debug(word, endl);
    }
    while (next_permutation(word.begin(), word.end()));
   

    //sort(words.begin(), words.end(), compare());
    //SORT(words);
    for (set<string>::iterator it = words.begin(); it != words.end(); ++it)
        printf("%s\n", it->c_str());

    debug(words.size(), endl);
}

int main()
{
    int nc;
    scanf("%d", &nc);
    while (nc-- > 0)
    {
        getInput();
        process();

        /*CLEAR GLOBAL VARIABLES!*/
        /*CLEAR GLOBAL VARIABLES!*/
    }

    return 0;
}
mgavin2
New poster
 
Posts: 21
Joined: Sat Jul 28, 2012 6:29 pm

Re: 195 - Anagram

Postby lbv » Tue Feb 12, 2013 11:13 pm

mgavin2 wrote:WA ... I'm not sure anymore with the hacks I've tried to work around :\


  • Did you check your program against the sample cases given in the problem statement?
  • Using a custom comparison method in next_permutation seems unnecessary. The order in which you generate the permutations don't have much importance if you end up placing the words in a set, which imposes its own order.
  • The functor in compare seems incomplete. You don't have a return statement at the end of that function, which means that the compiler is free to return anything if it reaches that point. Try configuring your compiler so that it shows you all warnings, which can help you catch this type of bug.
  • As you recognise, your code seems to have picked up a number of hacks along the way. That's never a good sign. I suggest you start from scratch, and try working out the problem in your head first until you're confident that you understand what the problem is asking for, and how to compute it. You'll notice that the solution doesn't require so much work and can be implemented succintly.
lbv
Learning poster
 
Posts: 54
Joined: Tue Nov 29, 2011 8:40 am

Re: 195 - Anagram

Postby brianfry713 » Wed Feb 13, 2013 12:06 am

Doesn't match the sample I/O.
brianfry713
Guru
 
Posts: 1856
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 195 - Anagram

Postby mgavin2 » Thu Feb 14, 2013 10:52 pm

lbv wrote:
  • Did you check your program against the sample cases given in the problem statement?
  • Using a custom comparison method in next_permutation seems unnecessary. The order in which you generate the permutations don't have much importance if you end up placing the words in a set, which imposes its own order.
  • The functor in compare seems incomplete. You don't have a return statement at the end of that function, which means that the compiler is free to return anything if it reaches that point. Try configuring your compiler so that it shows you all warnings, which can help you catch this type of bug.
  • As you recognise, your code seems to have picked up a number of hacks along the way. That's never a good sign. I suggest you start from scratch, and try working out the problem in your head first until you're confident that you understand what the problem is asking for, and how to compute it. You'll notice that the solution doesn't require so much work and can be implemented succintly.


Yeah, I'm going to start over... thanks for the suggestions thus far..

What annoys me is if there are similar characters in next_permutation, it seems to skip them
Code: Select all
    cin >> word;
    set<string> words;
    //SORT(word);
    do
    {
        words.insert(word);
        debug(word, endl);
    }
    while (next_permutation(word.begin(), word.end()));


with
Code: Select all
1
aAb


gives me
Code: Select all
aAb
abA
bAa
baA


Maybe I should just skip next_permutation and recursively generate them by hand :\
mgavin2
New poster
 
Posts: 21
Joined: Sat Jul 28, 2012 6:29 pm

Re: 195 - Anagram

Postby lbv » Fri Feb 15, 2013 12:46 am

mgavin2 wrote:What annoys me is if there are similar characters in next_permutation, it seems to skip them

It doesn't skip them. It's just that next_permutation works by calculating the next permutation in lexicographical order. If you initially feed it a string that is not the lexicographically lowest permutation, it will not produce all permutations; no surprises there. For more details, read its documentation.

mgavin2 wrote:Maybe I should just skip next_permutation and recursively generate them by hand :\

If you decide to do it, go ahead, it shouldn't be much harder. However, I can tell you that I solved it using only next_permutation with a custom comparator, and a simple mechanism to avoid duplicates (no set).
lbv
Learning poster
 
Posts: 54
Joined: Tue Nov 29, 2011 8:40 am


Return to Volume I

Who is online

Users browsing this forum: No registered users and 1 guest