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

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441257.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【CodeForces - 255C】Almost Arithmetical Progression (dp,离散化)

题干&#xff1a; Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: a1  p, wh…

Android手机mm开头的大文件,[2018年最新整理]2Android源代码编译命令m和mm和mmm以及make分析.doc...

[2018年最新整理]2Android源代码编译命令m和mm和mmm以及make分析老罗的新浪微博&#xff1a;/shengyangluo&#xff0c;欢迎关注&#xff01;在前文中&#xff0c;我们分析了Android编译环境的初始化过程。Android编译环境初始化完成后&#xff0c;我们就可以用m/mm/mmm/make命…

【CodeForces - 349C】Mafia(思维模拟,优秀的二分)

题干&#xff1a; One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a pl…

android新材料设计,android - 如何实现新材料BottomAppBar为BottomNavigationView - SO中文参考 - www.soinside.com...

解决了基本上&#xff0c;而不是试图迫使菜单的资源&#xff0c;我需要的布局&#xff0c;我用这个方法&#xff0c;而不是&#xff0c;我只是把使用“空”元素作为dglozano建议BottomAppBar内的LinearLayout。使用?attr/selectableItemBackgroundBorderless我也能做到这一点实…

【CodeForces - 1A】Theatre Square(水题,几何)(CODEFORCES,梦的开始)

题干&#xff1a; Theatre Square in the capital city of Berland has a rectangular shape with the size n  m meters. On the occasion of the citys anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the …

html教程是语音版,【HTML教程】HTML 语言简介

标签是一个容器标签&#xff0c;用于放置网页的主体内容。浏览器显示的页面内容&#xff0c;都是放置在它的内部。它是的第二个子元素&#xff0c;紧跟在后面。网页标题hello world

【POJ - 1664】放苹果 (递归经典题 或 dp 或 母函数)

题干&#xff1a; 把M个同样的苹果放在N个同样的盘子里&#xff0c;允许有的盘子空着不放&#xff0c;问共有多少种不同的分法&#xff1f;&#xff08;用K表示&#xff09;5&#xff0c;1&#xff0c;1和1&#xff0c;5&#xff0c;1 是同一种分法。 Input 第一行是测试数据…

html文本下一页,Javascript html2canvas + jsPDF 导出PDF,解决一半文字在上一页一半文字在下一页的问题...

var pdfContent document.getElementById("pdfDiv");var width pdfContent.offsetWidth; //获取dom 宽度var height pdfContent.offsetHeight; //获取dom 高度var canvas document.createElement("canvas"); //创建一个canvas节点var scale 3; //定义…

无法设置html过渡效果,html – CSS3过渡显示无阻止过度滚动

我假设你的弹出窗口是绝对定位的,所以你可以做以下事情&#xff1a;>隐藏时,将弹出窗口设置为巨大的负值.这会将其移出屏幕并摆脱滚动条.>在悬停时,将顶部设置为正确的值并转换不透明度值.>确保CSS转换规则仅适用于opacity属性.HTMLPopup go nowMy cats breath smells…

ACM 题目分类POJ(自用,精)

详情见博客&#xff1a;http://exp-blog.com/2018/06/28/pid-38/ 和博客https://blog.csdn.net/a1dark/article/details/11714009/ http://exp-blog.com/2018/06/10/pid-136/ https://blog.csdn.net/lyy289065406/article/details/6642573

html表格全屏显示,tableView滑动全屏显示

今天要分享的一个效果是在一个页面弹出视图展示一个tableview&#xff0c;然后手指滑动tableview时&#xff0c;视图随着tableview偏移量增加而慢慢增加&#xff0c;到达临界时&#xff0c;全屏显示&#xff0c;然后再次向下滑动时&#xff0c;当偏移量到达临界点&#xff0c;视…

【 CodeForces - 864B】Polycarp and Letters(水题,字符串,有坑)

题干&#xff1a; Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string sconsisting only of lowercase and uppercase Latin letters. Let A be a set of positions in the string. Lets call it pretty if following conditions are met:…

计算机网络65535,计算机网络1

1.网络基础&#xff1a;1.1 IT行业铁三角&#xff1a;os&#xff0c;web&#xff0c;sql 不管是哪个IT岗位都应该懂&#xff0c;1.2 开发铁三角&#xff1a;语言&#xff0c;数据结构算法&#xff0c;数据模式1.3 测试铁三角&#xff1a;需求&#xff0c;搭环境和设计用例&…

【CodeForces - 864C】Bus (模拟,有坑)

题干&#xff1a; A bus moves along the coordinate line Ox from the point x  0 to the point x  a. After starting from the point x  0, it reaches the point x  a, immediately turns back and then moves to the point x  0. After returning to the point…

大量html乱码seo,HTTPS改造之后网页错位乱码,影响SEO和正常访问,应该这样改

有一些朋友可能不太知道https改造怎么做&#xff0c;就学着网站的步骤进行&#xff0c;实际操作过程中可能会遇到很多问题。比如说有的会出现网页错位、页面乱码、后台功能无法使用的情况。昨天我们就有一个客户他自己做了https改造&#xff0c;但是造成后台无法上传图片的情况…

【NOIP2013积木大赛,NOIP2018铺设道路】积木大赛(思维,贪心)

题干&#xff1a; 题目描述 春春幼儿园举办了一年一度的“积木大赛”。今年比赛的内容是搭建一座宽度为nn的大厦&#xff0c;大厦可以看成由n块宽度为1的积木组成&#xff0c;第i块积木的最终高度需要是h_ihi​。 在搭建开始之前&#xff0c;没有任何积木&#xff08;可以看…

微型计算机中最小的单位,微型计算机中最小的数据单位是

微型计算机中最小的数据单位是比特。微型计算机&#xff0c;是指由微处理器作为CPU的计算机。由大规模集成电路组成的、体积较小的电子计算机。由微处理机(核心)、存储片、输入和输出片、系统总线等组成。特点是体积小、灵活性大、价格便宜、使用方便。这类计算机的普遍特征就是…

【每日算法】【图论】【最小边覆盖 最小路径覆盖 最小顶点覆盖 最大独立集 最大团】

最小边覆盖 最大独立集 |V| - 最大匹配数 这个是在原图是二分图上进行的 最小路径覆盖和最小边覆盖不同&#xff0c;不要求给的图是二分图&#xff0c;而是要求是N x N的有向图&#xff0c;不能有环&#xff0c;然后根据原图构造二分图&#xff0c;构造方法是将点一分为二&am…

【51Nod - 1344】走格子 (思维)

题干&#xff1a; 有编号1-n的n个格子&#xff0c;机器人从1号格子顺序向后走&#xff0c;一直走到n号格子&#xff0c;并需要从n号格子走出去。机器人有一个初始能量&#xff0c;每个格子对应一个整数Aii&#xff0c;表示这个格子的能量值。如果Aii > 0&#xff0c;机器人…

计算机算法在生物信息学中的应用,计算机算法在生物信息学中的应用综述.doc...

计算机算法在生物信息学中的应用综述摘 要&#xff1a;在人类基因组计划的推动下&#xff0c;生物信息学得到了人们的广泛关注&#xff0c;并呈现出数量多、计算量大等鲜明特征&#xff0c;因此要求在生物信息学中采用计算机算法&#xff0c;以提高生物信息学处理问题的效率。以…