传纸条
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
传纸条是一种在课堂上传递信息的老方法,虽然现在手机短信和QQ聊天越来越普及,但是手写的信息会让人感到一种亲切感。对许多学生而言,在学校里传递一些私秘性的信息是一种令人兴奋的打发时光的方式,特别是在一些令人厌烦的课堂上。
XX 和 YY 经常在自习课的时候传纸条来传递一些私密性的信息。但是他们的座位相隔比较远,传纸条要通过其他人才能到达对方。在传递过程中,难免会有一些好奇心比较强的同学偷看纸条的内容。所以他们想到了一个办法,对纸条内容进行加密。
加密规则很简单:多次在信息的任意位置随意的添加两个相同的字母。
由于使用英文交流显得比较高端,所以他们的纸条内容只有英文。
现在给你加密后的信息,请你还原出原始的内容。
输入
输入数据的第一行为一个正整数 T(T ≤ 30),表示共有 T 组测试数据。
接下来 T 行,每行为一个字符串,字符串仅包含小写英文字母,且保证原始字符串中不包含相邻两个相同的字母,字符串长度不超过200000。
输出
每组数据输出一行字符串,表示还原后的内容。
示例输入
1 ssilofaafveuuu
示例输出
iloveu
提示
删除掉aa后,又出现了ff,ff也要删除掉。
简单栈的应用,遍历字符串,当前字符与栈顶字符相同则出栈,否则该字符入栈,最后依次输出栈内元素即可,代码如下:
#include <iostream>
#include <cstdlib>
using namespace std;
typedef char anytype;
struct stacks
{
struct node //链栈
{
anytype data;
struct node *next;
}*head;
stacks() //初始化
{
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
}
bool empty() //判断栈空
{
if(head->next)
return false;
return true;
}
void push(anytype n) //入栈
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
p->data=n;
p->next=head->next;
head->next=p;
}
void pop() //出栈
{
struct node *p;
p=head->next;
if(p)
{
head->next=p->next;
free(p);
}
}
anytype top() //查询栈顶元素
{
if(!empty())
return head->next->data;
return 0;
}
};
int main()
{
ios::sync_with_stdio(false);
int t;
string str;
cin>>t;
while(t--)
{
stacks s;
cin>>str;
for(int i=str.length()-1;i>=0;i--) //从后向前遍历字符串
{
if(s.empty()||s.top()!=str[i]) //如果栈空或者栈顶元素与当前字符不等
s.push(str[i]); //入栈
else if(str[i]==s.top()) //如果相等则出栈(抵消)
s.pop();
}
while(!s.empty()) //依次弹出栈顶元素
{
cout<<s.top();
s.pop();
}
cout<<endl;
}
return 0;
}