日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 255B】Code Parsing(思维,字符串) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

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.

    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:

  • 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".

    題目大意:

    ? ? 給你一個字符串,定義兩種操作:1.如果遇到相鄰兩字符是yx的情況,就交換兩個字符串。2.如果遇到相鄰兩字符是xy的情況,就刪除(remove)這兩個字符。如果能操作1,就先操作1,如果整個字符串沒有可操作字符,那么執(zhí)行操作2。直到無法操作,輸出此時的字符串。。。

    解題報告:

    ? 這道題做的時候還是著急了、、直接就想著如果遇到了xy,那么就跳過。。。但是其實是不對的。。。仔細(xì)分析一下特征啊,從結(jié)果入手,,如果結(jié)果中有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 ;}

    ?

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

    總結(jié)

    以上是生活随笔為你收集整理的【CodeForces - 255B】Code Parsing(思维,字符串)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。