删掉连续的相同两个字母,直到不存在两个连续且相同的字母。
模拟一个栈即可。
#include<iostream> #include<cstdio> #include <cstring> #include <queue> #include <cmath> #include <algorithm> #include <cstdlib> #include <set> #include <vector> #include <map> using namespace std; #define eps 1e-7 #define INF 0x3fffffff #define L(u) (u<<1) #define R(u) (u<<1|1) #define MID(l,r) ((l+r)>>1) #define M 1000000009 const int maxn=200001; char s[maxn]; char stk[maxn]; int main() { int top=0; memset(stk,0,sizeof(stk)); scanf("%s",s); stk[top++]=s[0]; int len=strlen(s); for(int i=1;i<len;i++) { if(stk[top-1]==s[i]) { top--; } else { stk[top++]=s[i]; } } stk[top++]=0; printf("%s\n",stk); return 0; }

本文介绍了一种使用栈来删除字符串中连续相同字母的方法,通过遍历字符串并检查当前字符是否与栈顶元素相同,实现高效地过滤重复字符。
2152

被折叠的 条评论
为什么被折叠?



