Codeforces Round #156 (Div. 2)——B
3735 단어 Codeforces
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 string s, 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 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 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".
#include
#include
#include
#include
using namespace std;
#define maxn 1005000
char s[maxn];
int main()
{
gets(s);
int xcnt=0,ycnt=0;
for(int i=0;s[i];i++)
if(s[i]=='x') xcnt++;
else ycnt++;
int min= xcnt>ycnt? ycnt:xcnt;
xcnt-=min;
ycnt-=min;
if(xcnt>0)
{
for(int i=1;i<=xcnt;i++)
printf("x");
}
else
{
for(int i=1;i<=ycnt;i++)
printf("y");
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Codeforces 1287C Garland제목 링크:Codeforces 1287C Garland 사고방식: 우리기dp[i][j][0]와 dp[i][j][1]는 각각 i개가 홀수/짝수이고 앞의 i개 안에 j개의 짝수가 있는 상황에서 i개의 최소 복잡도.첫 번...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.