hdu B. Code Parsing

来源:互联网 发布:three.js源码 编辑:程序博客网 时间:2024/06/16 03:54

B. Code Parsing

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Problem Description

Little Vitaly loves different algorithms. Today he has invented a new algorithm just for you. Vitaly's algorithm works with strings, 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 strings.

 

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 most106 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 strings.

 

Sample Input
xyxyxy
 

Sample Output
xy
 


这道题 说 字符串仅由xy组成,有两个规则  如果有yx的话 可以转化为xy  如果是xy  就可以消去

也就是说只要我们无数次的运用第一条规则 就可以把x都放在前面,y都放在后面 然后再消去 xy  这样 本题就转化为 统计x与y数量多少的题目了。。。。orz

 

#include<iostream>#include<string>using namespace std;int main(){string str;int num;while(cin>>str){num=0;for(int i=0;i<str.length();i++){if(str[i]=='x')  num++;else  num--;}if(num>0) {for(int i=0;i<num;i++)  cout<<"x";}else{num=-num;for(int i=0;i<num;i++)  cout<<"y";cout<<endl;}}}


 

原创粉丝点击