UVa : 124 (Following Orders)


Title : Following Orders

Link : http://uva.onlinejudge.org/external/1/124.html

Tricky Lines :

  1. Orderings are printed in lexicographical (alphabetical) order
  2. Output for different constraint specifications is separated by a blank line.

Analysis :

  1. backtracking
  2. sort the variables
  3. keep an 2d array ‘constraint’
    constraint [m] [n] = true, if m < n
    so when constraint [n] [m] == true, u must not go further

Runtime : 0.008s

Solution :

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define pi acos(-1.0)
#define N 1000000
#define LL long long
using namespace std;

int variable_length;
bool constraint [26 + 5] [26 + 5];
bool avail [26 + 5];
char output [30];
char variable [30];

void reset ()
{
    for ( int i = 0; i < 26 + 5; i++ )
        memset (constraint [i], false, sizeof (constraint [i]));
    memset (avail, true, sizeof (avail));
}

bool check_constraint (int len, int in)
{
    for ( int i = 0; i < len; i++ ) {
        if ( constraint [in] [output [i] - 'a'] == true ) return false;
    }

    return true;
}

void bktk (int len)
{
    if ( len == variable_length ) {
        for ( int i = 0; i < len; i++ ) printf ("%c", output [i]);
        printf ("\n");
        return;
    }

    for ( int i = 0; i < variable_length; i++ ) {
        if ( avail [i] && check_constraint (len, variable [i] - 'a') ) {
            output [len] = variable [i];
            avail [i] = false;
            bktk (len + 1);
            avail [i] = true;
        }
    }
}

int main ()
{
    char input [500];
    bool blank = false;

    while ( gets (input) ) {
        variable_length = 0;

        for ( int i = 0; input [i]; i++ )
            if ( isalpha (input [i]) ) variable [variable_length++] = input [i];

        sort (variable, variable + variable_length);

        reset ();

        gets (input);
        char first;
        char second;

        for ( int i = 0; input [i]; i++ ) {
            if ( isalpha (input [i]) ) {
                first = input [i];
                i++;
                while ( !isalpha (input [i]) ) i++;
                second = input [i];
                constraint [first - 'a'] [second - 'a'] = true;
            }
        }

        if ( blank ) printf ("\n");
        blank = true;

        bktk (0);
    }

	return 0;
}

// @END_OF_SOURCE_CODE

One thought on “UVa : 124 (Following Orders)

  1. i could’t find any 8 worded palindromes for “following orders” please put som 8 word palindrome words for “following orders” up so people can find things that they need for a school assignment, or just in genral.

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