B. Code Parsing

4259 단어
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 strings, consisting of characters "x"and "y", and uses two following operations at runtime:
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.
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:
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.
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 106characters. 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:
string "yxyxy"transforms into string "xyyxy";
string "xyyxy"transforms into string "xyxyy";
string "xyxyy"transforms into string "xxyyy";
string "xxyyy"transforms into string "xyy";
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".
문제풀이 설명: 이 문제는 문자열 처리 문제로 x, y만 포함하는 문자열에 대해 두 가지 조작을 한다. 하나는 x, y의 위치를 교환하는 것이고, 하나는 x, y를 동시에 삭제하는 것이다.마지막으로 출력에 모든 x, y 쌍을 삭제한 후 남은 열을 삭제할 것을 요구합니다.여기에 교환이 존재하기 때문에 x, y의 위치가 처음에 어떻게 되었든지 간에 알파벳 하나만 남은 지경까지 삭제할 수 있음을 알아차렸다.이 문제는 통계 x, y의 개수로 전환하여 누가 많은지 판단하고 출력 차이의 문자열을 출력하면 된다.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

int prime[110001];
int table[500][500];

int sieve()
{
	int count=0;
    long long int i,j;
    prime[count++]=2;
    for(i=3;i<110001;i+=2)
    {
        if(prime[i]==0)
        {
            prime[count++]=i;
            for(j=i*i;j<110001;j+=2*i)
            {
				prime[j]=1;
			}
        }
    }
	return count;
}

int main()
{
	char a[1000002];
	int i,x=0,y=0,d;
	scanf("%s",a);
	for(i=0;i<strlen(a);i++)
	{
		if(a[i]=='x')
		{
			x++;
		}
		else
		{
			y++;
		}
	}
	d=x-y;

	if(d>0)
	{
		while(d--)
		{
			printf("x");
		}
	}
	else
	{
		while(d++)
		{
			printf("y");
		}
	}
    return(0);
}

좋은 웹페이지 즐겨찾기