Poj Solution 2937

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

#include <algorithm>
#include <cstdio>
#include <string.h>
#include <map>
#include <stack>
#include <memory.h>
#include <math.h>
#include <queue>
using namespace std;

int main( ) {
    int n, pos, t, s;
    char c[2];
    while( 1 ) {
        scanf( "%d", &n );
        
        if( n == 0 )
            break;
            
        pos=0, s=1;
        
        while( 1 ) {
            if( scanf( "%1s", c ) != 1 )
                break;
            if( strchr( "0123456789", c[0] ) ) {
                ungetc( c[0], stdin );
                break;
            }
            scanf( "%d", &t );
            
            if( c[0] == 'm' ) {
                if( t&1 )
                    pos = (n-pos)%n, s=-s;
            }
            else
                pos = (pos + t)%n;
        }
        
        if( pos == 0 && s == 1 )
            printf( "n" );
        else if( pos == 0 )
            printf( "m1n" );
        else if( s < 0 ) {
            if( n-pos +1 < 1 + pos )
                printf( "r%d m1n", n-pos );
            else
                printf( "m1 r%dn", pos );
        }else {
            if( pos < 2 + n-pos )
                printf( "r%dn", pos );
            else
                printf( "%m1 r%d m1n", n-pos );
        }
    }
    return 0;
}



											
This entry was posted in poj. Bookmark the permalink.