【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:
The input for the new algorithm is string?s, and the algorithm works as follows:
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
xOutput
xInput
yxyxyOutput
yInput
xxxxxyOutput
xxxxNote
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:
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 ;}?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的【CodeForces - 255B】Code Parsing(思维,字符串)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在银行存多少钱可以靠利息生活?银行存10
- 下一篇: 如何可以快速存到100万?存100万要多