第一行输入一个整数
代表数据组数。
接下来
行,每行一个仅由 'O' 和 'o' 构成的字符串
,字符串长度不超过
。
每组输出仅包含一行,输出一行字符串代表小鱼儿吐出的泡泡经过融合以后所剩余的泡泡。
1 ooOOoooO
oO
1 OOOOOOOOOOOOOOOooooooooooooooooooOOoOoOoOOOoOoOoOOoOooOoOOoOoOoOoOoOoOoOoOoOooOoOoOOoooOOOOoOOoooOOoOOOOOooOoOOOoOOoooOoOOOooOooooOoOooOoOooOoOooOoOOOOOOOOOOOOOOoOoOoOooOOoOooOoOOoOoOOOOooooOOOOOooooooOOOOOOoooooOoOooOoOoOoooOoOOOOoOoOoOOOOOOOOOOoOooOoOooOOoOOoOooOooOOoooOOOoOoOooOOooOoOOOoOOoOOOoOooOoOOOooOOoooOOoOOoOooOOOOoOooOoOoOoOooOoOoO
oOoOoOoOoOoO
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int round = in.nextInt();
// 处理多组测试用例
for (int i = 0; i < round; i++) {
String str = in.next();
System.out.println(bubble(str));
}
in.close();
}
public static String bubble(String str) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < str.length(); i++) {
char current = str.charAt(i);
// 循环处理当前字符与栈顶的反应(可能产生连续反应)
while (!stack.isEmpty()) {
char top = stack.peek();
// 两个小泡泡融合成大泡泡
if (top == 'o' && current == 'o') {
stack.pop(); // 弹出栈顶的'o'
current = 'O'; // 融合后变成大泡泡,继续检查是否与新栈顶反应
}
// 两个大泡泡爆炸消失
else if (top == 'O' && current == 'O') {
stack.pop(); // 弹出栈顶的'O'
current = 0; // 爆炸后消失,用0标记无需入栈
break; // 已爆炸,无需继续检查
}
// 其他情况(不反应),退出循环
else {
break;
}
}
// 如果当前字符未消失(不是爆炸后的状态),则入栈
if (current != 0) {
stack.push(current);
}
}
// 转换栈为字符串
return stackToString(stack);
}
// 栈转字符串工具方法
public static String stackToString(Stack<Character> stack) {
StringBuilder sb = new StringBuilder();
for (Character c : stack) {
sb.append(c);
}
return sb.toString();
}
}
#include <stdio.h>
#include <string.h>
//返回原串偏移指针,该指针到字符串结束存储结果字符串
char * bubbles(char *str){
char *rus_str = str;
int top;
int i = 0;
while (rus_str[i] != '\0') {
//与后一个字符是否相同
if(rus_str[i] == rus_str[i+1]){
//i和i+1均是'o'则i+1更新为'O',0~i-1后移一位(1~i)字符串变短,
//则rus_str向后偏移一位
if(rus_str[i] == 'o'){
rus_str[i+1] = 'O';
for (int j = i; j > 0; j--) {
rus_str[j] = rus_str[j-1];
}
rus_str++;
}
//i和i+1均是'o'则i+1更新为'O',0~i-1后移两位(2~i+1)字符串变短,
//则rus_str向后偏移两位
else {
for (int j = i+1; j > 1; j--) {
rus_str[j] = rus_str[j-2];
}
rus_str += 2;
}
//字符串更新后需从头开始索引清零
i = 0;
}
else i++;
}
return rus_str;
}
int main() {
int n;
char str[100001], rus_str[100001];
scanf("%d\n", &n);
while (n--) {
scanf("%s", str);
printf("%s\n", bubbles(str));
}
return 0;
} import java.util.Scanner;
import java.util.ArrayDeque;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int n =in.nextInt();
for (int i=0; i<n; i++) {
String str = in.next();
ArrayDeque<Character> deque = new ArrayDeque<Character>();
for (char c:str.toCharArray()) {
if (deque.isEmpty()) {
deque.push(c);
continue;
}
if (c=='o' && deque.getFirst()=='o') {
deque.pop();
if (deque.isEmpty()) {
deque.push('O');
continue;
}
if (deque.getFirst()=='O') {
deque.pop();
}
} else if (c=='O' && deque.getFirst()=='O') {
deque.pop();
} else {
deque.push(c);
}
}
while (!deque.isEmpty()) {
System.out.print(deque.removeLast());
}
System.out.println();
}
}
} T = int(input())
for _ in range(T):
s = list(map(str, input().strip()))
s_new = []
for i in s:
if i == 'o' :
if s_new and s_new[-1] == 'o':
s_new.pop() # 弹出前一个'o'
#两个‘o’合并成‘O’,并尝试和栈顶的‘O’抵消(重要)
if s_new and s_new[-1] =='O':
s_new.pop()
else:
s_new.append('O')
else:
s_new.append('o')
elif i == 'O':
if s_new and s_new[-1] == 'O':
s_new.pop() # 'O' 和 'O' 抵消
else:
s_new.append('O')
else:
s_new.append(i)
print(''.join(map(str, s_new)))
d第一次提交的时候,当前面的O是2个o合并的,再入栈一个O时,会直接入栈O,导致存在2个O没有抵消的情况发生,所以在2个o合并的过程中增加一个步骤,进行取栈顶操作。
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string ss;
for(int i=0;i<n;i++){
vector<char> c;
stack<char> s;
cin >> ss;
for (int j=0;j<ss.size();j++){
if(ss[j]=='o'){
if(s.empty() || s.top()!='o'){
s.push(ss[j]);
}else if(s.top() == 'o'){
s.pop();
if( s.size()!=0 && s.top()=='O'){
s.pop();
}else{
s.push('O');
}
}
}
else if(ss[j]=='O'){
if(s.empty() || s.top()!='O'){
s.push(ss[j]);
}else if(s.top() == 'O'){
s.pop();
}
}
}
while (!s.empty()) {
c.push_back(s.top());
s.pop();
}
for(int i=c.size()-1;i>=0;i--){
cout << c[i];
}
cout << endl;
}
}
// 64 位输出请用 printf("%lld") T = int(input())
for _ in range(T):
data = input()
stack = ["1"]
for i in range(len(data)):
if stack[-1]==data[i]=="o":
stack.pop()
stack.append("O")
if stack[-1]==stack[-2]:
stack.pop()
stack.pop()
elif stack[-1]==data[i]=="O":
stack.pop()
else:
stack.append(data[i])
print(*stack[1::],sep="") class Stack:
def __init__(self) -> None:
self.items = []
def push(self, x):
self.items.append(x)
def pop(self):
if not self.is_empty():
self.items.pop()
def is_empty(self):
return len(self.items) == 0
def peek(self): # 修正拼写
if not self.is_empty():
return self.items[-1]
return None
def tu_bubbles(bubbles):
# 初始化栈
stack = Stack()
# 遍历泡泡序列
for bubble in bubbles:
if bubble == "o":
if stack.peek() == "o":
stack.pop()
if stack.peek() == "O":
stack.pop()
else:
stack.push("O")
else:
stack.push("o")
elif bubble == "O":
if stack.peek() == "O":
stack.pop()
else:
stack.push("O")
# 输出最终结果
return "".join(stack.items)
# 读取输入
import sys
n = int(input())
for line in sys.stdin:
s = line.strip()
print(tu_bubbles(s))