Poj Solution 2491

http://poj.org/problem?id=2491

#include <stdio.h>
#include <string.h>


int next[400];
bool ishead[400];

char w1[400][100],w2[400][100];

int main()
{
    int cas, i, n, j, k = 0;
    

    scanf( "%d", &cas );
    
    while( cas-- )
    {
        scanf( "%d", &n );
        n--;        

        for( i=0; i<n; i++ )
        scanf( "%s%s",w1[i],w2[i] );

        for( i=0; i<n; i++ )
        next[i] = -1, ishead[i] = true;

        for( i=0; i<n; i++ )
        for( j=0; j<n; j++ )
        if( i != j && strcmp( w2[i], w1[j] ) == 0 )
        {
            next[i] = j, ishead[j] = false;
            break;
        }

        for( i=0; i<n; i++ )
        if(ishead[i])break;

        printf( "Scenario #%d:n", ++k );

        printf( "%sn", w1[i] );
        for( ; i >= 0; i = next[i] )
        printf( "%sn", w2[i] );

        printf( "n" );
    } 

    return 0;
}
											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *