Chủ Nhật, 16 tháng 12, 2012

Problem Code Parsing

Link: http://codeforces.com/problemset/problem/255/B

B. Code Parsing
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Little Vitaly loves different algorithms. Today he has invented a new algorithm just for you. Vitaly's algorithm works with string s, consisting of characters "x" and "y", and uses two following operations at runtime:
  1. Find two consecutive characters in the string, such that the first of them equals "y", and the second one equals "x" and swap them. If there are several suitable pairs of characters, we choose the pair of characters that is located closer to the beginning of the string.
  2. Find in the string two consecutive characters, such that the first of them equals "x" and the second one equals "y". Remove these characters from the string. If there are several suitable pairs of characters, we choose the pair of characters that is located closer to the beginning of the string.
The input for the new algorithm is string s, and the algorithm works as follows:
  1. If you can apply at least one of the described operations to the string, go to step 2 of the algorithm. Otherwise, stop executing the algorithm and print the current string.
  2. If you can apply operation 1, then apply it. Otherwise, apply operation 2. After you apply the operation, go to step 1 of the algorithm.
Now Vitaly wonders, what is going to be printed as the result of the algorithm's work, if the input receives string s.
Input
The first line contains a non-empty string s.
It is guaranteed that the string only consists of characters "x" and "y". It is guaranteed that the string consists of at most 106 characters. It is guaranteed that as the result of the algorithm's execution won't be an empty string.
Output
In the only line print the string that is printed as the result of the algorithm's work, if the input of the algorithm input receives string s.
Sample test(s)
input
x
output
x
input
yxyxy
output
y
input
xxxxxy
output
xxxx
Note
In the first test the algorithm will end after the first step of the algorithm, as it is impossible to apply any operation. Thus, the string won't change.
In the second test the transformation will be like this:
  1. string "yxyxy" transforms into string "xyyxy";
  2. string "xyyxy" transforms into string "xyxyy";
  3. string "xyxyy" transforms into string "xxyyy";
  4. string "xxyyy" transforms into string "xyy";
  5. string "xyy" transforms into string "y".

Thuật toán : Nhận thấy, khi còn tồn tại hai kí tự x, y thì luôn thực hiện thao tác 1, hoặc 2. Do đó thuật toán kết thúc khi chỉ còn một kí tự duy nhất.
Code:

/*
Coder : Nguyen Duc Tam
template for contest
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<vector>
#include<utility>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<stack>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<iomanip>
#include<ctime>
#include<cctype>

using namespace std;

/*
define for statement loop
*/
#define REP(i, start, end, step) for(int i = start; i < end; i += step)
#define DOWN(i, start, end, step) for(int i = start; i > end; i -= step)
#define FOR(it,c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())

/*
define operator bit
*/
#define L(x,i) ((x) << (i))
#define R(x,i) ((x) >> (i))
#define AND(a,b) ((a) & (b))
#define OR(a,b) ((a) | (b))
#define XOR(a,b) ((a) ^ (b))
#define NOT(a) (~(a))
#define SB(x,i) (OR((x), L(1, (i)))) // x | 1 << i
#define CB(x,i) (AND((x),NOT(L(1,(i))))) // x & ~(1 << i)
#define TB(x,i) (AND((x), L(1,(i)))) // x & (1 << i)

/*
define init data
*/
#define FILL(a,val) memset(a,val,sizeof(a));
#define INIT(a,l,r,val) REP(i,l,r,1) (a)[i] = val;

/*
define convert data
*/
#define DIG(c) (int)((c) - '0')
#define CHR(c) (char)((c) + '0')
#define LOW(c) (char)((c) + 32)
#define UPP(c) (char)((c) - 32)

/*
define math function
*/
#define sqr(a) (a) * (a)
#define abs(a) (a < 0 ? -(a) : (a))

/*
define find element in container very fast.
*/
#define LO(c,x) lower_bound(ALL(c),x)
#define UP(c,x) upper_bound(ALL(c),x)
#define IOF(c,x) distance((c).begin(), LO((c),x))
#define IOL(c,x) distance((c).begin(), UP((c),x))
#define IN(c,x) binary_search(ALL(c),x)
#define ER(c,x) equal_range(ALL(c),x)

/*
define geo
*/
#define DIS(p,q) sqrt(sqr(p.x - q.x) + sqr(p.y - q.y)) // khoang cach Euclide
#define DIM(p,q) abs(p.x - q.x) + abs(p.y - q.y) // khoang cach mahatan


/*
define constant
*/

#define EPS 1e-7
#define OO 1000000005
#define N 100005

#define X first
#define Y second

struct Point
{
double x, y;
Point(){}
Point(double x,double y):x(x), y(y){}
};

struct Node
{
int value;
Node* next;
Node(){}
Node(int value, Node* next):value(value),next(next){}
};

struct Edge
{
int src, dst, weight;
Edge(){}
Edge(int src, int dst, int weight):src(src), dst(dst), weight(weight){}

};

struct Compare
{

bool operator() (const int i, const int j)
{
return i > j;
}

/*
bool operator() (const Point& p, const Point& q)
{
return (p.x != q.x ? p.x < q.x : p.y < q.y);
}
*/
/*
bool operator()(const Edge& e, const Edge& f)
{
return (e.weight != f.weight ? e.weight > f.weight : e.src != f.src ?
e.src < f.src : e.dst < f.dst); // inverse weight
}
*/
};


const int DAY[13] = {-1,31,29,31,30,31,30,31,31,30,31,30,31};

const int dx4[4] = {-1, 0, 1, 0}; //U - R - D - L
const int dy4[4] = {0, 1, 0, -1};

const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; //U - UR - R - DR - D - DL - L - UL
const int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

typedef pair<int,int> II;
typedef pair<II,int> III;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned char UC;

vector<int>::iterator it, low, up; // using lower_bound, upper_bound
pair<vector<int>::iterator, vector<int>::iterator > bound; // using function equal_range(first, last, x)
Compare comp;
Point p[2];

string s;
int len = 0,x = 0 ,y = 0;

int main()
{
#define Off  true
if(Off)
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
cin >> s;
len = s.size();
REP(i,0,len,1)
if(s[i] == 'x') x++;
else y++;
if(x == y) cout << "" << endl;
else if( x > y)
{
REP(i,0,x-y,1) printf("%c",'x');
}
else
{
REP(i,0,y-x,1) printf("%c",'y');
}
return 0;
}



Không có nhận xét nào:

Đăng nhận xét