hdu B. Code Parsing

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 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 Input
  
  
x yxyxy
 

Sample Output
  
  
x y
 


这道题 说 字符串仅由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;
		}
	}
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值