#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
char str1[100001],str2[100001];
stack<char> s;
int main()
{
char ch;
scanf("%s",str1);
int len=strlen(str1);
while(!s.empty())
s.pop();
//s.push(str1[0]);
for(int i=0; i<len; i++)
{
//cout<<s.top();
if(!s.empty())
{//栈非空才能进行下一步比较
if(s.top()==str1[i])
{
s.pop();
}
else
s.push(str1[i]);
}
else
s.push(str1[i]);
}
int k=0,i;
if(s.empty())
{
printf("-1\n");
}
else
{
while(!s.empty())
{
str2[k]=s.top();
s.pop();
k++;
}
for(i=k-1;i>=0;i--)
printf("%c",str2[i]);
printf("\n");
}
return 0;
}
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
char str1[100001],str2[100001];
stack<char> s;
int main()
{
char ch;
scanf("%s",str1);
int len=strlen(str1);
while(!s.empty())
s.pop();
//s.push(str1[0]);
for(int i=0; i<len; i++)
{
//cout<<s.top();
if(!s.empty())
{//栈非空才能进行下一步比较
if(s.top()==str1[i])
{
s.pop();
}
else
s.push(str1[i]);
}
else
s.push(str1[i]);
}
int k=0,i;
if(s.empty())
{
printf("-1\n");
}
else
{
while(!s.empty())
{
str2[k]=s.top();
s.pop();
k++;
}
for(i=k-1;i>=0;i--)
printf("%c",str2[i]);
printf("\n");
}
return 0;
}
本文详细阐述了使用栈操作实现字符串反转的过程,包括初始化栈、遍历字符串、比较与弹出元素等关键步骤,最终通过字符串反转展示了栈在算法中的应用。
690

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



