题解 | #点击消除#
点击消除
https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXSIZE 300001 struct stack { char data[MAXSIZE]; int top; }; void push(struct stack* stac, char temp) { if (stac->top != MAXSIZE - 1) { stac->data[++stac->top] = temp; } } void pop(struct stack* stac) { if (stac->top != -1) { stac->data[stac->top--]; } } int main() { struct stack* stac = (struct stack*)malloc(sizeof(struct stack)); stac->top = -1; char* s = (char *)malloc(sizeof(char)*MAXSIZE); scanf("%s",s); int i = 0; while(s[i]!='\0'){ //相同不入栈,且出栈 if(s[i]==stac->data[stac->top]&&stac->top!=-1) pop(stac); //不同入栈 else push(stac,s[i]); i++; } if(stac->top == -1) printf("0"); else{ char* temp = (char *)malloc(sizeof(char)*((stac->top)+2)); int count = stac->top; for(int i = 0;i<=count;i++){ temp[stac->top+1] = stac->data[stac->top--]; } for(int i = 0;i<=count;i++){ printf("%c",temp[i]); } } }