【CodeForces - 255B】Code Parsing(思维,字符串)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/83903711

题干:

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.

Examples

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".

As a result, we've got string "y".

In the third test case only one transformation will take place: string "xxxxxy" transforms into string "xxxx". Thus, the answer will be string "xxxx".

题目大意:

    给你一个字符串,定义两种操作:1.如果遇到相邻两字符是yx的情况,就交换两个字符串。2.如果遇到相邻两字符是xy的情况,就删除(remove)这两个字符。如果能操作1,就先操作1,如果整个字符串没有可操作字符,那么执行操作2。直到无法操作,输出此时的字符串。。。

解题报告:

  这道题做的时候还是着急了、、直接就想着如果遇到了xy,那么就跳过。。。但是其实是不对的。。。仔细分析一下特征啊,从结果入手,,如果结果中有x和y,那么就一定还可以操作,,,因此最终一定是只有x或者只有y。那么究竟是哪一种呢?可以这么想啊,因为每一次remove操作都是去掉两个字符(一个x一个y),所以x和y都是成对删除的,,所以我们只需要看初始字符串哪个字符多就好了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 1e6 + 5;
char s[MAX];
int x,y;
int main()
{
	cin>>s;
	int len = strlen(s);
	for(int i = 0; i<len; i++) {
		if(s[i] == 'x') x++;
		if(s[i] == 'y') y++;
	}
	if(x > y) {
		for(int i = 1; i<=(x-y); i++) putchar('x');
	}
	else {
		for(int i = 1; i<=(y-x); i++) putchar('y');
	}
	return 0 ;
 }

错误代码:(WA10)(因为过不了  xxyy  这样的样例。)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 1e6 + 5;
char s[MAX];
int main()
{
	cin>>s;
	int len = strlen(s);
	for(int i = 0; i<len; i++) {
		if(s[i] == 'y' && s[i+1] == 'x') {
			i++;continue;
		}
		if(s[i] == 'x' && s[i+1] == 'y') {
			i++;continue;
		}
		printf("%c",s[i]);
	}
	return 0 ;
 }

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/83903711
今日推荐