Link to original topic
Describe
Small fish spit bubble, toot toot out. Small fish will spit out two kinds of bubbles: Big bubble "o", Small bubble "O".
Two adjacent small bubbles will melt into a large bubble, two adjacent large bubbles will explode.
(Yes, you're right, little bubbles and bubbles don't make any difference, and I don't know why.) )
Note: Merge from left to right.
For example: Oooooooo will become OO after a period of time.
Input
The data has multiple groups and is processed to the end of the file.
Each set of inputs contains a single row of ' O ' and ' O ' strings.
Output
Each set of outputs contains only one row, and the output line string represents the bubbles that the small fish spit out after fusion.
Sample input
Oooooooo
Sample output
Oo
Ideas
Using the stack simulation is good, water problem
Code
#include <bits/stdc++.h>#define ll long longusing namespace std;void check(char ans[], int len){ if(len == 0) return; while(ans[len] == ans[len-1]) { ans[len] = ‘\0‘; len--; if(ans[len] == ‘o‘) ans[len] = ‘O‘; else ans[len--] = ‘\0‘; if(len <= 0) return; }}int main(){ char s[102]; while(~scanf("%s", s)) { char ans[102]; int f = 1; while(f) { memset(ans,0,sizeof(ans)); ans[0] = s[0]; int j = 1; f = 0; int len = strlen(s); for(int i = 1; i < len; i++) { int j = strlen(ans); ans[j] = s[i]; check(ans, j); } memset(s,0,sizeof(s)); for(j = 0; ans[j] != ‘\0‘; j++) s[j] = ans[j]; s[j] = ‘\0‘; } printf("%s\n", s); } return 0;}
2018 National multi-school algorithm winter training Camp Practice Competition (second session) A. Spit Bubbles