1. 程式人生 > >【CodeForces - 255B】Code Parsing(思維,字串)

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

題幹:

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 ;
 }